Joe*_*ggs 2 php wordpress product categories woocommerce
我将以下参数定义为查询的一部分:
$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 );
Run Code Online (Sandbox Code Playgroud)
我需要从查询中排除名为“杂志”(别名“杂志”)或 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 );
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。
我在这里做错了什么?
产品类别是自定义分类法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 );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8063 次 |
| 最近记录: |