Query Posts with Multiple Taxonomies and Tax Terms
-
File: taxonomy-product_category.php
I’m trying to re-filter the main query based on a front-end filter/dropdown, where the user selects a location/term from the product_locations taxonomy.
From there I want all posts not in the selected location to be removed from the loop.
Loop below…
<?php //Setup query arguments to retrieve only posts that fall into both taxonomy categories / terms $args = array( 'post_type' => get_post_type(), 'orderby' => 'menu_order', 'order' => 'ASC', 'posts_per_page' => -1, 'no_found_rows' => true, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'product_category', 'field' => 'slug', 'terms' => array($category->slug), //Supply the current product_category terms ), array( 'taxonomy' => 'product_locations', 'field' => 'slug', 'terms' => array($current_location), //Add the product_locations term 'operator' => 'IN', ), ), ); //Should contain only the posts that fall under current post type and assigned to array of terms. $query = new WP_Query( $args ); ?> <?php //Now I want to loop through the tax terms from both "product_category" and "product_locations". <?php if ( $query->have_posts() ) : ?> <?php $i = 0 ?> <?php while ( $query->have_posts() ) : $query->the_post(); $i++ ?> <!-- Loop through only posts that fall into both taxonomy term(s) / term groups --> <?php endwhile; ?> <div class="fix"></div> <?php else : ?> <p>Sorry, no products found.</p> <?php endif; ?> <?php wp_reset_postdata() ?> <?php wp_reset_query() ?>
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Query Posts with Multiple Taxonomies and Tax Terms’ is closed to new replies.