Working With Taxonomy’s

I recently needed to work with a taxonomy that was created for a custom post type, where I needed to access the parent taxonomy.  I came across this same code in the wordpress support forums.  It should be useful when working with taxonomy’s in the future.


$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term

$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term

$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children

if ( ($parent->term_id!="" && sizeof($children)>0) ) {
// has parent and child
} else if ( ($parent->term_id!="") && (sizeof($children)==0) ) {
// has parent, no child
} else if ( ($parent->term_id=="") && (sizeof($children)>0) ) {
// no parent, has child
}

reference: https://wordpress.org/support/topic/if-current-taxonomy-has-child-andor-parent

Scroll to Top