Title: Checking hierarchical taxonomy
Last modified: June 28, 2018

---

# Checking hierarchical taxonomy

 *  [chinatownlee](https://wordpress.org/support/users/chinatownlee/)
 * (@chinatownlee)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/)
 * My goal is to display conditional content in an archive page if:
 * – Post belongs to the parent custom taxonomy and any subcategories of the same
   parent taxonomy.
 * Let’s say, custom taxonomy is called custom_taxonomy and it has a category ‘Animals’
   ID=01 (Parent)
    Subcategories of ‘Animals’ are ‘Beagle’ ID=02 and ‘Retriever’
   ID=03.
 * It’s easy with plain post categories and child subcategories.
 *     ```
       if (is_category('Animals') || cat_is_ancestor_of('Animals', get_query_var( 'cat' )) { echo 'Successs';
       ```
   
 * How do I check whether the post belongs to a subcategory of taxonomy parent category‘
   Animals’ ID=01?
 * The first part works fine, but I have like 50 subcategories and I’m certain there
   should be a better way than listing them one by one.
 * `is_tax('custom_taxonomy', 'Animals') ) ||`

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/page/2/?output_format=md)

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10442841)
 * You’re calling cat_is_ancestor_of() wrong. Both values passed to that function
   have to be either a taxonomy term object or an integer ID. A string like ‘Animals’
   is invalid and won’t work.
 * To see how it works, just check the [codex page](https://codex.wordpress.org/Function_Reference/cat_is_ancestor_of).
   Assuming that the main parent ID that you want to check os ‘1’, you’d just use
   this:
 *     ```
       if( cat_is_ancestor_of( 1, get_query_var( 'cat' ))) {
           // Do stuff...
       } // if ()
       ```
   
 *  Thread Starter [chinatownlee](https://wordpress.org/support/users/chinatownlee/)
 * (@chinatownlee)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10442858)
 * This is exactly why I’m using integers, just as you said for the usual categories.
 * I just thought this would better illustrate my question, which probably was not
   good idea.
 * The question though remains unanswered.
 * What about the custom hierarchical taxonomy?
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10442868)
 * I did miss that part, sorry. Too busy looking at the function to see what was
   going on.
 * For costom taxonomies. you can use term_is_ancestor_of(). It’s almost the same,
   but you tell it what taxonomy you’re referring to.
 * [https://codex.wordpress.org/Function_Reference/term_is_ancestor_of](https://codex.wordpress.org/Function_Reference/term_is_ancestor_of)
 *  Thread Starter [chinatownlee](https://wordpress.org/support/users/chinatownlee/)
 * (@chinatownlee)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10442885)
 * I had a look at that before and it didn’t work.
 * I think the reason for this is that it’s not about the terms – there is a single
   term in my case -> advert
 * The name of custom taxonomy is advert_category.
 * `( term_is_ancestor_of(380, $term, 'advert_category')`
 * 380 – ID of the parent taxonomy category.
    Didn’t work.
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10442894)
 * Just on the off chance… Are you 100% Sure that the term that you’re using and
   the one with the ID of 380 are both ‘advert_category’ terms? I know that it seems
   like a silly question, but I’ve gotten things easier then that wrong before.
 * If they are both correct, I’d try digging into the code for that function to 
   see what it does, and see if you can find out why it’s failing. That is the function
   to use, so there’s got to be a reason for it failing if it’s not meant to.
 *  Thread Starter [chinatownlee](https://wordpress.org/support/users/chinatownlee/)
 * (@chinatownlee)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10443593)
 * Well it’s definitely not working irrespective of the theme I use.
 * Custom hierarchical taxonomy: Family
    1st Parent term: Jacksons (id=1) Child 
   term: Mother Child term: Father
 * The following code placed in archive.php (TwentySeventeen) returns nothing when
   visiting the actual archives.
 *     ```
       <?php
       if ( term_is_ancestor_of( 1, $term, 'family') ) : ?>
           <p>Jacksons</p>
       <?php endif; ?>
       ```
   
 * What’s working
    `is_tax( 'family', 1 );`
 * But that means this would need to be repeated for all of the child terms >50.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10444578)
 * Umm, isn’t proper use of an ancestor_of function in whatever form sorta off topic?
   😉
 * You want the invert of that, yes? Does this term’s ancestor ID == 1? Every term
   object has a parent ID field. Get the term of that ID. Is _that_ term’s ID ==
   1? If yes, there’s your answer. If no, does ID == 0? If not, rinse and repeat.
   If yes, you’ve reached the top level term and there was no match.
 * ^^ code that
 *  Thread Starter [chinatownlee](https://wordpress.org/support/users/chinatownlee/)
 * (@chinatownlee)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10444663)
 * I feel the irony, but I could not get your actual answer.
 * I want to show the same message when visiting each and every archive of the children
   term of the parent term with the ID = 1, just as I wrote earlier.
 * Are you implying I should use something else?
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10446149)
 * I’ve just done some tests, and term_is_ancestor_of() works exactly as it’s meant
   to. My example…
 * Create taxonomy:
 *     ```
       register_taxonomy ('ancestry', 'page', array (
           'label' => 'Ancestry',
           'hierarchical' => true
       ));
       ```
   
 * Set up terms for the ‘ancestry’ taxonomy (listed with ID’s):
 *     ```
       19 -    First Level
       20 -        Second Level
       22 -            Third Level
       21 -        Second Level 2
       23 -    First Level 2
       ```
   
 * Code to check function:
 *     ```
       echo "<p>Second Level is ancestor of First Level - '".term_is_ancestor_of (19, 20, 'ancestry')."'</p>";
       echo "<p>Third Level is ancestor of First Level - '".term_is_ancestor_of (19, 22, 'ancestry')."'</p>";
       echo "<p>Third Level is ancestor of First Level 2 - '".term_is_ancestor_of (23, 22, 'ancestry')."'</p>";
       ```
   
 * Results:
 *     ```
       Second Level is ancestor of First Level - '1'
       Third Level is ancestor of First Level - '1'
       Third Level is ancestor of First Level 2 - ''
       ```
   
 * So the first two checks come back as true, and the third is false, which is exactly
   what the results are supposed to be.
 * From this, all that I can think of is that either you’ve got the ID’s the wrong
   way around, or that your taxonomy isn’t set up as hierachical.
 *  Thread Starter [chinatownlee](https://wordpress.org/support/users/chinatownlee/)
 * (@chinatownlee)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10446693)
 * Thanks for the response, [@catacaustic](https://wordpress.org/support/users/catacaustic/).
 * Could you please edit the code to not include the child term exactly like in 
   the example in [documentation](https://codex.wordpress.org/Function_Reference/term_is_ancestor_of)?
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10446698)
 * If you have to use $term, then I’d suggest something like this, to test what 
   you are ctualyl using for $term.
 *     ```
       echo "<p>Term is '".$term."'</p>";
       echo "<p>Checking term:  '".term_is_ancestor_of (19, $term, 'ancestry')."'</p>";
       ```
   
 *  Thread Starter [chinatownlee](https://wordpress.org/support/users/chinatownlee/)
 * (@chinatownlee)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10450762)
 * Thanks for the effort [@catacaustic](https://wordpress.org/support/users/catacaustic/)
   🙂
 * What I am using the term_is_ancestor_of for is exact replication of the example
   provided in [codex](https://codex.wordpress.org/Function_Reference/term_is_ancestor_of#Examples):
 * > The code snip below checks to see if the term called ‘Music’ (ID 4) for the
   > taxonmy ‘Sound’ is being processed, and if so, presents a wp_nav_menu for the
   > Music archive page, and any subterms of Music (e.g. jazz, classical.)
 * It is this part that is my actual goal:
 * > and any subterms of Music (e.g. jazz, classical.)
 *     ```
       <?php 
         if (term_is_ancestor_of(4, $term, 'sound') or is_term(4, 'sound')):  ?>
         <div id="music_subnav_menu" class="subnav_menu">
           <?php wp_nav_menu( array('menu' => 'Music' )); ?>
         </div>
       <? endif; ?>
       ```
   
 * which is just not working.
    -  This reply was modified 7 years, 11 months ago by [chinatownlee](https://wordpress.org/support/users/chinatownlee/).
    -  This reply was modified 7 years, 11 months ago by [chinatownlee](https://wordpress.org/support/users/chinatownlee/).
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10451557)
 * Do you have a custom taxonomy of ‘sound’ set up with a parent that has the ID
   of 4? If not, it will fail because that’s what it’s asking for. You can’t just
   cut-and-past that. You need to make sure that you have the correct values being
   passed in to that function.
 *  Thread Starter [chinatownlee](https://wordpress.org/support/users/chinatownlee/)
 * (@chinatownlee)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10451696)
 * Whenever I’m visiting child terms of 448 and 451 I see both messages ‘Jacksons’
   and ‘Watsons’ and as I now understand this works as intended.
 *     ```
       if (term_is_ancestor_of(448, $term, 'family') or is_term(448, 'family')) {
       echo '<p>Jacksons</p>'; }
       if (term_is_ancestor_of(451, $term, 'family') or is_term(451, 'family')) {
       echo '<p>Watsons</p>'; }
       ```
   
 * I a have a bunch of different parent terms with child terms and I hoped I could
   use this function for delivering conditional content in a way I wanted.
 * Any advice on how could I achieve the goal of displaying only the ‘Watsons’ message
   when visiting any of the child terms of Parent ID 451?
    -  This reply was modified 7 years, 11 months ago by [chinatownlee](https://wordpress.org/support/users/chinatownlee/).
    -  This reply was modified 7 years, 11 months ago by [chinatownlee](https://wordpress.org/support/users/chinatownlee/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/#post-10452418)
 * Let’s see if I can comment this time without getting my thinking backwards 🙂
 * Don’t bother with a series of `if ()` conditional logic. Would the term ID 451’
   s name be ‘Watsons’ by any chance? If not, the string could be stored as term
   meta. Then if you got any term’s ancestors with get_ancestors(), the last object
   in the returned array is the top level term. Its name is in that object, or get
   the stored meta by term ID. Get it and output the value. Then a couple lines 
   of code will work for any number of top level terms and their children, and you
   do not need to add to your code if additional lines of descendants were to be
   added.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/page/2/?output_format=md)

The topic ‘Checking hierarchical taxonomy’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [hierarchical taxonomy](https://wordpress.org/support/topic-tag/hierarchical-taxonomy/)
 * [subcategory](https://wordpress.org/support/topic-tag/subcategory/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 16 replies
 * 3 participants
 * Last reply from: [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/checking-hierarchical-taxonomy/page/2/#post-10452965)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
