How to display a banner only in a specific category? A developer guide to advanced conditions and parent checking.
EN

How to check if a post belongs to a specific taxonomy term? (Has_term, is_tax)

5.00 /5 - (24 votes )
Last verified: March 1, 2026
Experience: 5+ years experience
Table of Contents

A common task for a developer is to display an element (e.g., an ad banner) only if the post belongs to a specific category or taxonomy (e.g., ‘Movie Genre: Comedy’).

In WordPress, we have two main functions for this that are often confused: is_tax() and has_term(). The difference between them is crucial for your theme’s logic.


1. Is_tax() vs has_term() – What’s the diff?

Is_tax() – Page context (archive)

Use this to check what page the user is viewing.

  • Is the user currently on the “Horror” category archive page?
if ( is_tax( 'genre', 'horror' ) ) {
    echo 'You are on the Horror list!';
}

This function will return false if you are on a Single Post page, even if that post is actually a horror movie!

Has_term() – Post context (single)

Use this to check what is assigned to a specific post.

  • Is this specific movie a “Horror”?
if ( has_term( 'horror', 'genre' ) ) {
    echo 'This post is a horror movie.';
}

This function is most often used inside The Loop or in the single.php file.


2. The hierarchy problem (parents and children)

The biggest challenge is that has_term() only checks exactly the term you specify.

Assume this structure:

  • Movies (Taxonomy: movie_genre)
    • Comedy (ID: 10)
      • Romantic (ID: 11)
      • Dark Comedy (ID: 12)

If you have a post assigned ONLY to “Romantic”, checking:

has_term( 'comedy', 'movie_genre' ) 

will return FALSE. Why? Because the post technically doesn’t have the “Comedy” checkbox checked, only “Romantic”.

Solution: Helper function for descendants

To check if a post belongs to “Comedy” OR any of its subcategories, we need to write our own helper function.

/**
 * Checks if a post belongs to a term or its children.
 *
 * @param int|string|array $term_id_or_slug ID or Slug of the parent term.
 * @param string $taxonomy Taxonomy name.
 * @param int|null $post_id Post ID (optional).
 * @return bool
 */
function wppoland_in_term_tree( $term_id_or_slug, $taxonomy, $post_id = null ) {
    $post_id = $post_id ? $post_id : get_the_ID();
    $term    = get_term_by( is_numeric($term_id_or_slug) ? 'id' : 'slug', $term_id_or_slug, $taxonomy );

    if ( ! $term ) {
        return false;
    }

    // 1. Check direct assignment (fastest)
    if ( has_term( $term->term_id, $taxonomy, $post_id ) ) {
        return true;
    }

    // 2. Get all children of the term
    $children = get_term_children( $term->term_id, $taxonomy );
    
    if ( is_wp_error( $children ) || empty( $children ) ) {
        return false;
    }

    // 3. Check if the post has any of the children
    return has_term( $children, $taxonomy, $post_id );
}

Usage:

// In single.php
if ( wppoland_in_term_tree( 'comedy', 'movie_genre' ) ) {
    get_template_part( 'partials/banner-comedy' );
}

Now this works for both “Comedy” and “Romantic Comedy”.


3. Performance optimization

The get_term_children() function performs a database query. If you use this in a loop displaying 50 posts, you are making 50 extra SQL queries.

How to optimize? If your category structure rarely changes, you can cache the children IDs in the Transients API. However, in 2026, servers are fast enough that for simple structures this isn’t strictly necessary unless you have thousands of subcategories.

A more important optimization is passing the ID instead of the Slug. WordPress has to look up the ID from the Slug anyway, so providing the ID (e.g., 10) saves one lookup query.

// Faster (ID)
has_term( 10, 'movie_genre' );

// Slower (Slug -> requires lookup)
has_term( 'comedy', 'movie_genre' );

Summary

  1. Use is_tax() only to check if you are on an archive page.
  2. Use has_term() to logically check individual posts.
  3. Remember that has_term doesn’t inherit – if you need “category and its children” logic, use a custom helper function.

This is the foundation of building dynamic themes that react to content context.

What should you know about How to check if a post belongs to a specific taxonomy term? (Has_term, is_tax)?
How to check if a post belongs to a specific taxonomy term? (Has_term, is_tax) is relevant when you want a more stable WordPress setup, better performance, and fewer production issues.
How do you implement How to check if a post belongs to a specific taxonomy term? (Has_term, is_tax)?
Start with a baseline audit, define scope and constraints, then roll out improvements in small, testable steps.
Why is How to check if a post belongs to a specific taxonomy term? (Has_term, is_tax) important?
The biggest gains usually come from technical quality, clear information structure, and regular verification.

Need an FAQ tailored to your industry and market? We can build one aligned with your business goals.

Let’s discuss

Related Articles