Get parent id by term id

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

1
1

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.

Leave a Comment