29

Is there any way to get only parent terms from custom taxonomy or category?

5 Answers 5

53

Yes, just pass in the parent parameter to get_terms when you call it, as Michael pointed out.

Since WP 4.5 this is the recommend usage:

$myterms = get_terms( array( 'taxonomy' => 'taxonomy_name', 'parent' => 0 ) );

Prior to WP 4.5 this was the default usage:

$myterms = get_terms( 'taxonomy_name_here', array( 'parent' => 0 ) );

Will return all terms that have a parent value of 0, ie. top level terms.

3
  • It returns empty array for custom taxonomies :(
    – Mamaduka
    Commented Aug 2, 2011 at 13:14
  • 1
    Are the terms in that taxonomy associated with a post(or custom type)? If not, you'll need to pass along the hide_empty parameter, setting that to 0 also, so you're able to see terms currently not used.
    – t31os
    Commented Aug 2, 2011 at 13:25
  • Note that this will only get parent level 1 , the "mother" term. To retrieve all ancestors, use get_ancestors(TERM_ID, TAXONOMY, 'taxonomy') developer.wordpress.org/reference/functions/get_ancestors
    – jave.web
    Commented Apr 13, 2017 at 6:07
2

use the 'parent' parameter:

http://codex.wordpress.org/Function_Reference/get_terms

or

http://codex.wordpress.org/Function_Reference/get_categories

2

for woocommerce email templates use the following:

$terms = get_the_terms( $_product->id , 'product_cat');
    if($terms) {
        foreach( $terms as $term ) {
            $term = get_term_by("id", $term->parent, "product_cat");
            if ($term->parent > 0) {
                $term = get_term_by("id", $term->parent, "product_cat");
            }
            $cat_obj = get_term($term->term_id, 'product_cat');
            $cat_name = $cat_obj->name;
        }
    }
echo '<br />('. $cat_name . ')';
1
  • 6
    please add some explanation, on how your code could solve the question. OP didn't make a question about woocommerce email templates.
    – iEmanuele
    Commented Sep 2, 2013 at 14:34
1
 $archive_cats= get_terms( 'archivecat', 'orderby=count&hide_empty=0&parent=0' );
4
  • 3
    How is this different than the (already accepted) answer proposed more than two years ago?
    – tfrommen
    Commented Oct 24, 2013 at 8:31
  • did you see comments on the (already accepted) answer ? if no more answers are not useful why the question still opened ?! Commented Nov 12, 2013 at 13:42
  • There was a single query with regard to terms not showing up, that was unrelated to the original question, i addressed that issue in a reply comment(as it had no bearing on the validity or correctness of the answer already provided).
    – t31os
    Commented Jan 28, 2014 at 13:48
  • 1
    Please address that in an edit. Explain your code.
    – kaiser
    Commented Feb 3, 2014 at 22:19
0

For this demonstration, we will assume that we have a taxonomy called "Books". And we might have a hierarchy of:

Fiction (id: 699)
 - This is a Fiction Story.
Non-Fiction
 - Title of Non-fiction
Fantasy
  -  ...

We want to get the parent term of the taxonomy "Books" of the current post.

/* Get the Parent Taxonomy Term Name by post Id */

function get_parent_term_by_post_id($taxname, $taxid=null){

if(isset($taxid)):

    //Get by term Id passed in function
    $parent_tax = get_term_by('id', $taxid, $taxname);

    return $parent_tax ->name; //use name, slug, or id

else:

     //Get by PostId of current page
     $terms = wp_get_post_terms( get_the_id(), $taxname);

     $tax_parent_id = $terms[0]->parent;

     if($tax_parent_id == 0):
      $tax_parent_id = $terms[0]->parent; //get the next parent ID
     endif; 

     $parent_tax = get_term_by('id', $tax_parent_id , $taxname);

     return $parent_tax ->name; //use name, slug, or id

endif;

}

Then you just run on you template like this:

//enter custom tax name. will grab by post id 
echo get_parent_term_name_by_post_id('books'); //returns Fiction

or

//by term ID 
echo get_parent_term_name_by_post_id('books', 699); //returns Fiction

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.