本文介绍了从WP_Query中排除WooCommerce产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在查询中定义了以下参数:
I've got the following args defined as a part of my 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.
I need to exclude a category called Magazines (slug "magazines") or ID 351 from the query.
我一直试图包含'category__not_in'=>array('magazines')
,所以看起来像这样:
I've been trying to include 'category__not_in' => array('magazines')
, so it looks like this:
$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
.
Product categories are a custom taxonomy product_cat
.
因此,您需要通过以下简单的 tax_query
方法进行操作:
So you need to do it in a simple tax_query
this way:
$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 );
这篇关于从WP_Query中排除WooCommerce产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!