Popular Categories

How to hide a WooCommerce Category from Search Result?

Do you want to exclude specific WooCommerce Category from WordPress search? By default, WordPress search includes all posts, pages and products in the search results. In this post, we will show you how to easily exclude specific WooCommerce category from WordPress search results.

function hide_rentals_from_search_pre_get_posts( $query ) {
   if (!is_admin() && $query->is_main_query() && $query->is_search()) {
       $query->set( 'post_type', array( 'product' ) );

//in the current case i want to hide "rental" category, you can easily replace this with some other product category slug
       $tax_query = array(
            'taxonomy' => 'product_cat',
            'field'   => 'slug',
            'terms'   => 'rentals', 
	    'operator' => 'NOT IN',

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