admin管理员组

文章数量:1318770

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.

本文标签: hooksHow to exclude a taxonomy from shop amp search page wooCommerce