How do you get the ID of the parent term of the current child term assigned to a post. For a custom taxonomy?
Custom taxonomy: “location”
Say we are on a post assigned to “Japan” how do I get the ID of the parent called “Countries”? For this case it doesn’t matter if it takes the highest parent or just the direct parent.
I found this:
$term_id = 21; $child_term = get_term( $term_id, 'location' ); $parent_term = get_term( $child_term->parent, 'location' );
But it doesn’t work.
If you just need IDs,
get_ancestors will return an array of parents for any type of object:
$term_id = 21; $ancestors = get_ancestors( $term_id, 'location' );