The wording of the title is terrible, but I am not sure how else to word it. I have two taxonomies: product_categories and brands.

I'm on the page /product-categories/grocery. So the term here is "Grocery". Posts are tagged with both taxonomies. I am trying to output all of the Brands and posts associated with the term "Grocery".

I'm currently editing taxonomy-product_categories.php. This is what I want the page to look like for the above url.

Brand A
- Product 1
- Product 2
Brand B
- Product 1
- Product 2


But right now, the issue with my code, is that I'm getting all Brands, I am unsure how to make it relate specifically to the term page that I'm on, and in this example that's Grocery.

This is my code:

$taxonomy = 'brands';
$categories = get_categories( array(
    'taxonomy'          => $taxonomy,
    'hide_empty'        => true,

foreach ($categories as $category) :

    $title = $category->name;
    $link = get_category_link($category->cat_ID);

    echo '<h4><a href="' . $link . '">' . $title . '</a></h4>';

    $tax_query = array(
            'taxonomy' => $taxonomy,
            'field'    => 'slug',
            'terms'    => $category,
            'operator' => 'IN',

    // global $post;
    $args = array(
        'post_type'   =>  'products',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'title',
        'order' => 'asc',
        'tax_query'   => $tax_query

    $products = new WP_Query( $args );

    if ( $products->have_posts() ) :
    echo '<div class="tax-products">';

        echo '<div class="tax-product-item x-column x-sm x-1-4">';

        while ($products->have_posts()) : $products->the_post();
        // $products = get_posts($args);

        // foreach ( $products as $post ) : setup_postdata( $post );

            $product_title = get_the_title();
            $product_link = get_the_permalink();

            echo '<li class="all-brand-product-item"><a href="' . $product_link . '">' . $product_title . '</a></li>';

        // endforeach; 

        // wp_reset_postdata();

        echo '</div>'; // end tax-product-item

    echo '</div>'; // end tax-products
    else :
        echo "No Products Found.";



I feel like somehow if the get_categories array could somehow get the term_id then maybe it would work the way I need it to.

  • How do you know which 'brands' are associated with 'groceries'? Are you trying to control just the list of terms in the menu? Or are you trying to do a full taxonomy intersection of the posts that are being displayed? – Tom J Nowell Jun 2 '17 at 16:29
  • That's the problem, I don't know how to show that 'brands' are associated with 'groceries', that's the missing piece to making this work. – Darren Bachan Jun 2 '17 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.