![](/img/trans.png)
[英]Product variation WP_Query with a product category in Woocommerce
[英]Exclude a WooCommerce product category from a WP_Query
我在查询中定义了以下参数:
$args = apply_filters('woocommerce_related_products_args', array(
'post_type' => 'product',
'author' => $artist,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
)
) );
$products = new WP_Query( $args );
我需要从查询中排除名为Magazines(子弹“ magazines”)或ID 351的类别。
我一直试图包括'category__not_in' => array('magazines')
,所以看起来像这样:
$args = apply_filters('woocommerce_related_products_args', array(
'post_type' => 'product',
'author' => $artist,
'post_status' => 'publish',
'category__not_in' => array('magazines'),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
)
) );
$products = new WP_Query( $args );
但这似乎不起作用。
我在这里做错了什么?
产品类别是自定义分类法product_cat
。
因此,您需要通过以下简单的tax_query
:
$args = apply_filters('woocommerce_related_products_args',
array(
'post_type' => 'product',
'author' => $artist,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
),
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug', // Or 'name' or 'term_id'
'terms' => array('magazines'),
'operator' => 'NOT IN', // Excluded
)
)
)
);
$products = new WP_Query( $args );
您应该使用category__not_in
值作为这样的数组
$query = new WP_Query( array( 'category__not_in' => array( 'magazines' ) ) );
有关详细信息,请检查此
所以请更新您的代码
$args = apply_filters('woocommerce_related_products_args', array(
'post_type' => 'product',
'author' => $artist,
'post_status' => 'publish',
'category__not_in' => array('magazines')
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
)
) );
$products = new WP_Query( $args );
让我知道它是否对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.