How to Hide WooCommerce Product from Search Results

woocommerce hide product from searchIn this post I want to share a quick WooCommerce hide product from search results snippet that you can add to the theme or a plugin to help hide any product you wish to hide from search results. This code snippet uses the same approach as the previous tutorial on how to hide WooCommerce product without price.

If you want to hide specific product from the search results, you should consider adding this snippet to your theme and set the product visibility to hidden since the snippet uses the value of the visibility in a meta query to hide the product  from search.

WooCommerce Hide Product from Search Results

To hide products from search you should add this code snippet to the functions.php of your theme or you can add this code the plugin files if you want to hide product from search using a custom plugin.

if ( ! function_exists( 'njengah_hide_products_from_search' ) ){
  function njengah_hide_products_from_search( $query = false ) {
    if(!is_admin() && is_search()){
      $query->set( 'meta_query', array(
        'relation' => 'OR',
        array(
          'key' => '_visibility',
          'value' => 'hidden',
          'compare' => 'NOT EXISTS',
        ),
        array(
          'key' => '_visibility',
          'value' => 'hidden',
          'compare' => '!=',
        ),
      ));
    }
  }
}
add_action( 'pre_get_posts', 'njengah_hide_products_from_search' );

After adding this code snippet you should also enure the product you want to hide should have the visibility set to hidden.

Similar Articles