使用 WP_Query 从 WooCommerce 中的特定品牌获取产品

Get products from a specific brand in WooCommerce using a WP_Query

在 WooCommerce 中,我试图在主页中显示来自特定品牌的产品,例如特色产品部分。我尝试了下面的代码,但产品不属于该品牌。

这是我试过的:

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

有什么建议吗?

你应该使用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 );