Reputation: 1409
In woocommerce I need to display products that are members of 2 categories. I am using the following code:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 200, 'product_cat' => 'Washington', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
I would like to display products which are members of both the categories "Washington" and "Small Single"
I do not want to display all products from "Washington", then all products from "Small Single", I would like to display all products which are members of both categories
How would I amend the above code to include the category "Small Single"?
Upvotes: 1
Views: 3615
Reputation: 896
Had the same requirement, turned out to be very simple
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 200,
'product_cat' => 'washington+small-single',
'orderby' => 'rand'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
Upvotes: 1
Reputation: 2810
It is possible with category__and
parameter.
1) USE Category IDs
of "Washington" and "Small Single"
2) In your $args
Adjust below parameter.(I assume Washington catId = 2
and Small Single CatID = 6
)
$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );
3) It will only display Products contained in Both category.
I hope, It will help you!
Upvotes: 2