Skip to content
Advertisement

Get products from a specific brand in WooCommerce using a WP_Query

In WooCommerce, I am trying to display products from a specific brand in my home page like featured products section. I tried the code below, but the products not belong to that brand.

This is what I tried:

$args = array(
    'post_type' => 'product',                              
    'product_brand' => 'armitage',
    'orderby' => 'rand',
    'posts_per_page' => 4,
);
$loop = new WP_Query( $args );

Any suggestions?

Advertisement

Answer

You should use tax_query

$args = array(
    'post_type' => 'product',
    'orderby' => 'rand',
    'posts_per_page' => 4,
    'tax_query'      => array( array(
        'taxonomy'        => 'pa_brand-attr',
        'field'           => 'slug',
        'terms'           =>  'armitage',
        'operator'        => 'IN',
    ) )
);
$loop = new WP_Query( $args );
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement