从循环短代码 Woocommerce 中排除产品类别

时间:2021-06-29 13:35:28

标签: php wordpress woocommerce hook

我正在尝试排除或删除“自定义”产品类别,它在商店页面上也可以正常工作,在主页中,当我尝试从简码中排除时,该类别的产品不显示,但在另一个简码中显示同一个页面(主页)当我尝试在另一个容器中仅显示另一个类别的产品时它给我带来了一些问题它不起作用它向我显示来自“最新”的所有产品,不仅来自我过滤的特定类别,还可以有人帮我解决吗? 谢谢

add_action( 'woocommerce_shortcode_products_query' , 'exclude_cat_shortcodes');
 
function exclude_cat_shortcodes($query_args){
 
    $query_args['tax_query'] =  array(array( 
            'taxonomy' => 'product_cat', 
            'field' => 'slug', 
            'terms' => 'custom', // slug category that I want to exclude
            'operator' => 'NOT IN'
        )); 
 
    return $query_args;
}

2 个答案:

答案 0 :(得分:1)

为了防止 tax_query 覆盖,请执行此操作

$query_args['tax_query'][] = array( 
    'taxonomy' => 'product_cat', 
    'field' => 'slug', 
    'terms' => ['custom'], // slug category that I want to exclude
    'operator' => 'NOT IN'
); 

答案 1 :(得分:0)

要从查询中排除类别或帖子类型,请尝试将 slug 类别放在数组中?这是为了允许排除多个类别/帖子类型。

变化:

'terms' => 'custom'

到:

'terms' => ['custom']

相关问题