2

I've got a custom post type that has hierarchical taxonomies set up. For an example, my post type of "project" has categories of

 A
   A.1
 B
 C

I'm trying to get the taxonomies displayed as classes on <li> items for each post, but I just want the top level parents shown. For the post I'm looking at, it is categorized as A.1 and C, but I'd like to return A and C.

I'm using 'parent' => 0 in the args, however its giving me A.1 and C. I have also tried using 'hide_empty' => 0 but that doesn't seem to help.

Here's my code:

 function project_get_item_classes($post_id = null) {
     if ($post_id === null)
         return;
     $_terms = wp_get_post_terms($post_id, 'construction_type', array( 'parent' => 0, 'hide_empty' => 0 ));
     foreach ($_terms as $_term) {
         echo " " . $_term->slug;
     }
 }

3 Answers 3

7

wp_get_post_terms doesn't accept 'parent' or 'hide_empty' parameters in it's arguments array, only 'orderby','order' and 'fields' but you are on the right track, simply add a conditional check before you echo out the slug:

function project_get_item_classes($post_id = null) {
     if ($post_id === null)
         return;
     $_terms = wp_get_post_terms($post_id, 'construction_type');
     foreach ($_terms as $_term) {
        if ($_term->parent == 0) //check for parent terms only
            echo " " . $_term->slug;
     }
 }
1
  • Thanks, I had the args confused with another function. Now, the only issue is that the parent category is not being applied to the child. IE category A should be applied to a post for category A.1. Before this snippet category A.1 was being applied, but now it is just blank.
    – javy
    Mar 25, 2013 at 20:44
5

Simply order by parent...

$terms = wp_get_post_terms(  $ID , $tax, array( 'orderby' => 'parent', 'order' => 'ASC' ) );

$myparent = $terms[0];
3

Thanks to help from @Bainternet (here, and also his input which led to the answer in this question), I was able to scrape together the code.

// 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 project_get_item_classes($taxonomy, $results = 1) {
    // get terms for current post
    $terms = wp_get_object_terms( get_the_ID(), 'work_type' );
    // set vars
    $top_parent_terms = array();
    foreach ( $terms as $term ) {
        //get top level parent
        $top_parent = get_term_top_most_parent( $term->term_id, 'work_type' );
        //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 ) {
        echo " " . $term->slug;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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