Using WooCommerce to create an eCommerce solution inside WordPress is great, although retrieving the correct products can sometimes be a little tricky, especially products from two or more categories. This is made easier using Taxonomy Queries for WordPress as WooCommerce has done an excellent job with their product categories taxonomy.
This is a quick blog post to share a snippet which allows me to retrieve say 12 products from either Product Category One or Product Category Two. This works really well on websites that might want to pull in products from say clothing and accessories.
Multiple Products Category Query Snippet
What we use below is a tax_query, we set an array of details that we wish to query against and then let WordPress do the hard work. First of all we set a relation, if you want 12 products from either category you have to set it as OR, but if you wanted only products from two categories, like a featured category AND product category one, you could set it to AND.
After that we set the taxonomy name, in the case of WooCommerce it is product_cat and then we set the field we wish to check against, I have found slug to be the most reliable. Then we set the slug of the term we wish to query against. This returns 12 products ready for us to loop through and mark up. After we have done our new WP_Query and the loop remember to reset the post data so that the rest of the page works correctly.
$args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => 'product-category-one' ), array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => 'product-category-two' ) ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); // Do your markup. endwhile; } wp_reset_postdata();
Select Products From Featured Category Snippet
This next snippet shows you how to grab products from a product category and a featured product category. This can be a nice way to allow users to set featured products on the homepage for say a clothing category. The query below is the same as the one before except the relation is AND instead of OR. This means that products are only retrieve if they are in the Product Category AND Featured Category.
$args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => 'product-featured' ), array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => 'product-category-two' ) ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); // Do your markup. endwhile; } wp_reset_postdata();
As normal we reset the post data to make sure the rest of the page works correctly.
I hope this quick post has proven useful and you start to use WooCommerce more and more, as it’s incredibly powerful once you get to grips with the fact it’s just a plugin so you can still use the power of WordPress, this is probably the same reason bbPress moved over to a plugin as it was originally a stand alone piece of software.
Comments