Get parent id by term id

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