从 wordpress 全球搜索结果中排除 woocommerce 产品类别 [英] Exclude a woocommerce product category from wordpress global search results

查看:31
本文介绍了从 wordpress 全球搜索结果中排除 woocommerce 产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从 wordpress 搜索结果中排除某些帖子类别:

I'm using this code to exclude some post category from the wordpress search results :

function SearchFilter($query)
{
    if ($query->is_search)
    {
        $query->set('cat', '-709,-710,-614');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');

我的问题是它不适用于 woocommerce 类别并且产品没有被过滤.我该如何过滤一些 woocommerce 类别?

My problem is that it doesn't work for woocommerce categories and products are not filtered. How can I filter some woocommerce categories too ?

推荐答案

你能替换你的类别吗?希望这对您有所帮助.

Can you replace your category. hope this help you.

function wpse188669_pre_get_posts( $query ) {

   if (

       ! is_admin()

&& $query->is_main_query()

&& $query->is_search()

   ) {

       $query->set( 'post_type', array( 'product' ) );

       // set your parameters according to

     // https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

       $tax_query = array(

           array(

               // likely what you are after

               'taxonomy' => 'product_cat',

               'field'   => 'slug',

               'terms'   => 'category-2',

               'operator' => 'NOT IN',

           ),

       );

       $query->set( 'tax_query', $tax_query );

}

}

add_action( 'pre_get_posts', 'wpse188669_pre_get_posts' );

这篇关于从 wordpress 全球搜索结果中排除 woocommerce 产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆