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.

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

share|improve this question
add comment

4 Answers 4

up vote 14 down vote accepted

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

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

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

share|improve this answer
    
It returns empty array for custom taxonomies :( –  Mamaduka Aug 2 '11 at 13:14
    
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 Aug 2 '11 at 13:25
    
Thanks, need to dig into other args too –  Mamaduka Aug 2 '11 at 14:15
add comment

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 . ')';
share|improve this answer
1  
please add some explanation, on how your code could solve the question. OP didn't make a question about woocommerce email templates. –  iEmanuele Sep 2 '13 at 14:34
add comment
 $archive_cats= get_terms( 'archivecat', 'orderby=count&hide_empty=0&parent=0' );
share|improve this answer
3  
How is this different than the (already accepted) answer proposed more than two years ago? –  t f Oct 24 '13 at 8:31
    
did you see comments on the (already accepted) answer ? if no more answers are not useful why the question still opened ?! –  ashraf mohammed Nov 12 '13 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 Jan 28 at 13:48
1  
Please address that in an edit. Explain your code. –  kaiser Feb 3 at 22:19
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.