Determine Term depth

Not trying to bump my rep, but I found my own answer. get_ancestors allows you to get the hierarchy of any item. Since terms can only have 1 parent, this is all we need: the number of items in this list equates to the term depth level, and even provides term ids.

Usage:

$ancestors = get_ancestors( $term_id, 'custom-taxonomy-slug' );
print_r( $ancestors ) ; // array( 0 => 15, 1 => 45 ) - 3rd level term

Leave a Comment