I have custom taxonomy some thing like :

  • Item - A
    • Item A-1
    • Item A-2
      • Item A-2-1
      • Item A-2-2

Now i want to get what is the number of hierarchical number of Item A-2, if we start from 0 it should be 1 and so on..

Please help me.

1 Answer 1


You can use get_ancestors() to get an array of parents, which you can then count.

$term_id = 42;
$ancestors = get_ancestors( $term_id, 'category' );
if( ! empty( $ancestors ) ){
    echo count( $ancestors );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.