Search code examples
wordpresswoocommerceproductcustom-taxonomytaxonomy-terms

Get WooCommerce mixed 10 products from multiple product categories


In WooCommerce, I'm trying to get 10 products from multiple categories and this is my code:

$args = array(
        'post_type'      => 'product',
        'posts_per_page' => 10,
        'product_cat'    => 'ring,watch,earring'
    )
);
$loop = new WP_Query( $args );

I'm getting 10 products from the first category only.

Is there any way to get 10 mixed products from multiple categories?


Solution

  • Updated

    Your code is a bit outdated, instead try the following to query products from specific product category terms using the argument "orderby" set to "rand" (random order by) to make them mixed up:

    $loop = new WP_Query( array(
        'post_type'      => 'product',
        'posts_per_page' => 10,
        'post_status'    => 'publish',
        'orderby'       => 'rand',
        'tax_query'      => array( array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array('ring', 'watch', 'earring'),
         ) ),
    ) );
    

    Tested and work.