Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 BREAKFAST
   x Cereal
   x Eggs
  LUNCH
     Hamburger
   x Pizza
  DINNER
     Fish
        Bass
      x Salmon
        Trout
     Lasagna

How can I do this?

share|improve this question
add comment

4 Answers 4

up vote 5 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
add comment

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

so 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
1  
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
show 6 more comments

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
add comment
/**
 * 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
add comment

Your Answer

 
discard

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.