Site icon Experience Wordpress Developer Online – Design Develop and Maintenance Support

How to remove products of a specific category from search results in Woocommerce

In WooCommerce when you search some product name in searchbar it will display list of product realted to search but if you want to remove some product of a specific category from search results than how to do it. Here is a solution. Let’s have a look.

In this example i was search a “product” caption in searchbar. It dispaly all product related to my search. but i don’t want to display “cat1” category product in my search result. so I will acheive this by below code. Let’s have a look.

Add below line of code at the end of your theme’s functions.php file of your active child theme (or theme) and Save the file. so it will remove all product realted to “cat1” category.


function remove_product_specific_category_searchresult( $query ) {
   if (
       ! is_admin()

&& $query->is_main_query()

&& $query->is_search()

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

       $tax_query = array(

           array(

               'taxonomy' => 'product_cat',

               'field'   => 'slug',

               'terms'   => 'cat1', // Category slug here

               'operator' => 'NOT IN',
           ),

       );
       $query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'remove_product_specific_category_searchresult' );

Exit mobile version