How to Hide WooCommerce Product from Search Results

Hide WooCommerce Product from Search ResultsIn 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.

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.

See Also: How To Hide The Stock Status in WooCommerce