Skip to content

Instantly share code, notes, and snippets.

@5iDS
Created November 28, 2013 23:50
Show Gist options
  • Select an option

  • Save 5iDS/7699644 to your computer and use it in GitHub Desktop.

Select an option

Save 5iDS/7699644 to your computer and use it in GitHub Desktop.
WP determine the topmost parent of a term
function get_term_top_most_parent($term_id, $taxonomy){
// start from the current term
$parent = get_term_by( 'id', $term_id, $taxonomy);
// climb up the hierarchy until we reach a term with parent = '0'
while ($parent->parent != '0'){
$term_id = $parent->parent;
$parent = get_term_by( 'id', $term_id, $taxonomy);
}
return $parent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment