• Resolved germangallo

    (@germangallo)


    I’m working on a page that show a lot of products. My structure is

    Product (custom post type)
    –>Brand (custom taxonomy)
    ——>Brand A (custom term)
    ——>Brand B (custom term)
    –>Department (custom taxonomy).
    ——>Department A (custom term)
    ——>Department B (custom term)

    Each product always has to have one brand AND one department. However, there may be a few products in each brand.

    So, I need to do a list that shows me the all the products that matches both a specific brand AND a specific department, and also that let me print the name of the brand. i.e: In department A we have 3 products that matches Brand A.

    Is there any way to do it? I’m able to filter either one a brand or a department, but not both at the same time.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello

    Please use the below code stuff this will helps you to “get posts that matches specific terms of multiple custom taxonomies”
    Note:Please replace the post type name,taxonomy and its term with your respected values.

    <?php //Setup query arguments to retrieve only posts that fall into both taxonomy categories / terms
    $args = array(
    	'post_type'      => 'your-post-type-name',
    	'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',
    		),
    	),
    );
    
    $query = new WP_Query( $args ); ?>
    
    <?php if ( $query->have_posts() ) : ?>
    
    	<?php while ( $query->have_posts() ) : $query->the_post();?>
    
    		<?php the_title(); ?>
    
    	<?php endwhile; ?>
    
    	<?php else : ?>
    
    		<p>Sorry, no products found.</p>
    
    	<?php endif; ?>
    
    <?php wp_reset_postdata() ?>
    
    <?php wp_reset_query() ?>

    Thanks

    Thread Starter germangallo

    (@germangallo)

    Hi Clarion, that’s work great! Sorry for the dalay in my answer, but I was out of my city. Thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get posts that matches specific terms of multiple custom taxonomies.’ is closed to new replies.