最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - How to exclude a taxonomy from shop & search page wooCommerce?

programmeradmin1浏览0评论

In my wooCommerce shop, I've 2 taxonomy

1. Product Category
2. Services Category

Shop page is showing up all products from both categories. I want to exclude products from SHOP & Search Results page which are in the Services Category.

Is this thing car be achieved by pre_get_posts ? but I think it will not on work with the wooCommerce shop page.

Let me know what is the best approach to do this. any hooks or actions for this.

Thank you. Any help would be appreciated.

In my wooCommerce shop, I've 2 taxonomy

1. Product Category
2. Services Category

Shop page is showing up all products from both categories. I want to exclude products from SHOP & Search Results page which are in the Services Category.

Is this thing car be achieved by pre_get_posts ? but I think it will not on work with the wooCommerce shop page.

Let me know what is the best approach to do this. any hooks or actions for this.

Thank you. Any help would be appreciated.

Share Improve this question asked Oct 20, 2020 at 18:45 Danish MohammedDanish Mohammed 135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This might help you with Shop page listing:

https://docs.woocommerce/document/exclude-a-category-from-the-shop-page/

And for Search page, here is a snippet that might help:

function exclude_services_category_products_from_search( $query ) {

   if ( is_search() && !is_admin() ) {

        $array_with_service_category_id = [];
        $tax_query = $query->get( 'tax_query' ) ?: [];

        $tax_query[] = [
            'taxonomy' => 'product_cat',
            'terms' => $array_with_service_category_id,
            'field' => 'term_id',
            'operator' => 'NOT IN'
        ];
    
        $query->set( 'tax_query', $tax_query );
    
    }
    return $query;
}
add_filter( 'pre_get_posts', 'exclude_services_category_products_from_search' );

Just add the Service Category id in the $array_with_service_category_id array.

发布评论

评论列表(0)

  1. 暂无评论