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.

1 Answer 1

1

This might help you with Shop page listing:

https://docs.woocommerce.com/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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.