Query Products by Category ID, and Multiple Categories (3 posts)

  1. dankfresh
    Posted 2 years ago #

    I'd like to query products by a category ID, not slug, and also filter products that belong to more than 1 category. Something similar to WP_Query's category__and

    This is the syntax that works:

    $args = array(
            'posts_per_page' => -1,
            'product_cat' => 'category-slug-here',
            'post_type' => 'product',
            'orderby' => 'title',
    	$the_query = new WP_Query( $args );
    	// The Loop
    	while ( $the_query->have_posts() ) {
    		echo '' . get_the_title() . '<br /><br />';

    Is it possible to filter the product categories by ID? Can I filter products by using an AND condition so they must belong to multiple categories (without 2 loops?)


  2. dankfresh
    Posted 2 years ago #

    So, didn't realize this but product_cat can take multiple arguments, separated by commas. This acts like an "OR" statement, though, and doesn't produce the desired effect. I would like to find products that specifically belong to both.

  3. pou7777
    Posted 2 years ago #

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic