How to Fix WooCommerce Hidden Products Showing in Search Results
How to Fix WooCommerce Hidden Products Showing in Search Results

We have received several emails from WooCommerce users complaining about hidden products appearing in weird places like the search results or related products section.
The reason behind this is WordPress does not acknowledge the product visibility setting of WooCommerce.
The hidden status of the product is unique to WooCommerce, not the entire WordPress ecosystem.
So, even if you set a product or product category to be hidden on WooCommerce, the hidden products may still appear when WooCommerce products are returned (queried) outside of WooCommerce.
Don’t worry if you don’t understand what this means. You don’t need to know what causes this issue to fix it.
We have written a PHP code snippet that will prevent hidden WooCommerce products from appearing in search results and other weird places like related products. All you need to do is copy this code and paste it into functions.php file in your WordPress theme.
Table of Contents
The first thing we need to do to use add the code snippet that will prevent WooCommerce Hidden Products Showing in Search Results is to open the functions.php file.
There are many different ways you can edit functions.php file but the best way is using the Theme Editor option on the WordPress dashboard.
-
Open functions.php file.
After logging into the WordPress dashboard, choose the Appearance menu from the left sidebar and then click on the Theme Editor sub-menu.
Then click on the functions.php under the Theme Files title on the right side to open it on the theme editor.
-
Adding the code snippet
After scrolling down to the bottom of functions.php file, copy the following code snippet and paste it at the end of the functions.php
if ( ! function_exists( 'woo_hidden_search_query_fix' ) ){
function woo_hidden_search_query_fix( $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', 'woo_hidden_search_query_fix' );
This is all you need to do to prevent hidden WooCommerce products from unexpectedly showing up in the search results. Please let us know if this doesn’t work for you.