List Terms in Category

I want to list all terms of a specific taxonomy used in a category.

Like I have a taxonomy called ‘Manufacturer’ and a category ‘Shoes’. Each post in this category has a term like ‘Nike’, ‘Adidas’ or ‘Reebok’ and of course there are some posts having the same term.

Read More

And on the archive page I want to list all the terms used in this category and not terms like ‘American Apparel’, used in in the category “Shirts”.

I hope you understand my problem and have a good idea for solving this problem.

Thanks in advance,
Timo

Related posts

Leave a Reply

2 comments

  1. Since you are in a category the your query will get the posts of that category, you only need to add ‘posts_per_page’ => -1 to that query so it will get you all the posts in that category and not the default “at most” number.

    so something like:

     query_posts( $query_string . '&posts_per_page=-1' );
    

    this will will give you all of the posts of that category as we said before, next you loop through the posts and collect the terms in to an array

    $Manufacturer = array();
    while (have_posts()){
        // loop over the posts and collect thier term ID's into $Manufacturer array
        the_posts();
        $terms =wp_get_object_terms($post->ID,'Manufacturer');
        if (count($terms)) {
            foreach ($terms as $term){
                if (!in_array($term->term_id,$Manufacturer)){
                    $Manufacturer[] = $term->term_id;
                }
            }
        }
    }
    //here you have an array $Manufacturer with the id's of only terms with posts in the current category
    //so you can do what ever you want with them.
    //rewind the posts so you could display them normally without creating a new WP_query object
    rewind_posts();