WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How would I get the top-level parent of a given term?

I am using wp_get_object_terms to get taxonomy terms on posts, but instead of showing all tagged terms, I only want to show tagged terms' top-level parents.

So if these are my selected terms, I only want to show Breakfast, Lunch and Dinner.

   x Cereal
   x Eggs
   x Pizza
      x Salmon

How can I do this?

share|improve this question
up vote 13 down vote accepted

Thanks to Ivaylo for this code, which was based on Bainternet's answer.

The first function (get_term_top_most_parent) accepts a term ID and taxonomy and returns the the term's top-level parent (or the term itself, if it's parentless); the second function (hey_top_parents) works in the loop, and, given a taxonomy, returns the top-level ancestors of a post's terms.

// 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;

// so once you have this function you can just loop over the results returned by wp_get_object_terms

function hey_top_parents($taxonomy, $results = 1) {
    // get terms for current post
    $terms = wp_get_object_terms( get_the_ID(), $taxonomy );
    // set vars
    $top_parent_terms = array();
    foreach ( $terms as $term ) {
        //get top level parent
        $top_parent = get_term_top_most_parent( $term->term_id, $taxonomy );
        //check if you have it in your array to only add it once
        if ( !in_array( $top_parent, $top_parent_terms ) ) {
            $top_parent_terms[] = $top_parent;
    // build output (the HTML is up to you)

    foreach ( $top_parent_terms as $term ) {

          $r = '<ul>';
          $r .= '<li><a href="'. get_term_link( $term->slug, $taxonomy ) . '">' . $term->name . '</a></li>';
          $r .= '</ul>';

    // return the results
    return $r;

share|improve this answer

Here is a simple function that will get you the top most parent term of any given term:

function get_term_top_most_parent( $term_id, $taxonomy ) {
    $parent  = get_term_by( 'id', $term_id, $taxonomy );
    while ( $parent->parent != 0 ){
        $parent  = get_term_by( 'id', $parent->parent, $taxonomy );
    return $parent;

Once you have this function you can just loop over the results returned by wp_get_object_terms:

$terms =  wp_get_object_terms( $post->ID, 'taxonomy' );
$top_parent_terms = array();
foreach ( $terms as $term ) {

    //Get top level parent
    $top_parent = get_term_top_most_parent( $term->ID, 'taxomony' );

    //Check if you have it in your array to only add it once
    if ( !in_array( $top_parent->ID, $top_parent_terms ) ) {
        $top_parent_terms[] = $top_parent;
share|improve this answer
Thanks! I'm trying this now. FYI I think you're missing a close paren in line 1 of the function. – supertrue Aug 3 '11 at 13:19
do I need to add $taxonomy as a second parameter in $top_parent? – supertrue Aug 3 '11 at 13:20
I added the $taxonomy parameter but am getting an error when I use this code, here: gist.github.com/1122631 – supertrue Aug 3 '11 at 13:33
yeah you do. i updated the answer. – Bainternet Aug 3 '11 at 13:33
your function seems broken your foreach loops are overlapping try this: pastebin.com/u48dxzap and if you still get an error paste all of you code and i'll check – Bainternet Aug 3 '11 at 17:36

Since 3.1.0, get_ancestors() is available. It returns an array of ancestors from lowest to highest in the hierarchy.

share|improve this answer
This is the correct answer in my opinion. – numediaweb Jul 5 '15 at 10:14
This is the best answer. – mark Jul 22 '15 at 21:28
 * Get top level term
function get_top_level_term($term,$taxonomy){
    if($term->parent==0) return $term;
    $parent = get_term( $term->parent,$taxonomy);
    return get_top_level_term( $parent , $taxonomy );
share|improve this answer
Please add an explanation along with your code. – s_ha_dum Oct 18 '13 at 4:43

I had the same problem and I solved easily. Check this out:

Define $taxonomy. It can be the slug of the taxonomy you want to get the data. After doing this, you can simply do this:

    $postterms = wp_get_post_terms($post->ID, $taxonomy);   // get post terms
    $parentId = $postterms[0]->parent;                      // get parent term ID
    $parentObj = get_term_by('id', $parentId, $taxonomy);   // get parent object 

Now you got something like this:

object(stdClass)#98 (11) {
  string(8) "Esportes"
  string(8) "esportes"
  string(17) "noticiaseditorias"
  string(0) ""
  string(3) "raw"

And you can use $parentObj to get slug, name, id, whatever. Just by using $parentObj->slug or $parentObj->name as exemple.

share|improve this answer

Maybe this helps: get_ancestors( $object_id, $object_type );


share|improve this answer

Easiest way:

$rootId = end( get_ancestors( $term_id, 'my_taxonomy' ) );
$root = get_term( $rootId, 'my_taxonomy' );
echo $root->name;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.