从 WP_Query 中排除 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 (slug "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 );
但这似乎不起作用。
我做错了什么?
你应该像这样使用 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 );
让我知道它是否适合你。
产品类别是自定义分类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 );
我在查询中定义了以下参数:
$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 (slug "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 );
但这似乎不起作用。
我做错了什么?
你应该像这样使用 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 );
让我知道它是否适合你。
产品类别是自定义分类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 );