8

I have the id of a term. Which is the function to call to get the parent id of the term?

1

2 Answers 2

19

If you already have the term, like the term is an actual object you could use $term->parent. Otherwise you can do something like this:

$term = get_term($id, 'YOUR_TAXONOMY_HERE');
$termParent = ($term->parent == 0) ? $term : get_term($term->parent, 'YOUR_TAXONOMY_HERE');

The 2nd portion of this is a shorthand if-else, IF it doesn't have a parent, then we assign it itself, otherwise we get the parent.

1
  • 2
    If only the ID is needed (and not term object), modify line #2 to: $termParent = $term ? $term->parent : false;. This will either give you the parent ID, or false. Not the object. Also saves a query. Oct 13, 2014 at 20:29
0

To get all parents of "category" you can use :

get_ancestors($term_id, 'category', 'taxonomy');

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.