简体   繁体   中英

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?

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM