Why Hidden and Private WooCommerce Products Still Appear in Search Results (And How to Fix It)
Content
If you've ever set a WooCommerce product to Hidden or Private only to find it still appears in your site's search results, you're not alone. This is a common and frustrating issue that can lead to customers finding products you intended to keep out of the public eye. This guide explains why this happens and provides the most effective solutions.
Understanding the Problem: Hidden vs. Private vs. Search
First, it's important to understand what the different visibility settings are supposed to do:
- Hidden: Products marked as "Hidden" should not appear on shop pages, category pages, or in WooCommerce-specific search results. However, they remain accessible via their direct URL.
- Private: A standard WordPress "Private" post status should hide the product from everyone except logged-in administrators. It should not appear anywhere on the front end.
The core of the issue is that these settings primarily affect WooCommerce's own product queries. Your site's default WordPress search (?s=product) often uses a different, broader query that does not respect these visibility settings by default. This is a known, long-standing issue in the WooCommerce ecosystem.
Common Solutions and Workarounds
Since this behavior is not fully handled by WooCommerce core, implementing a fix requires adding custom code to your site. Here are the most common and effective methods.
Solution 1: Code to Exclude Hidden Products from WordPress Search
The following code snippet modifies the main WordPress search query to exclude products with "Hidden" catalog visibility. You can add this code to your child theme's functions.php file or use a code snippets plugin.
function exclude_hidden_products_from_search( $query ) {
if ( ! is_admin() && $query->is_search() && $query->is_main_query() ) {
$meta_query = $query->get( 'meta_query' );
if ( empty( $meta_query ) ) {
$meta_query = array();
}
// Exclude products hidden from catalog
$meta_query[] = array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
);
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'exclude_hidden_products_from_search' );
Solution 2: Code to Exclude Private Products from All Front-End Queries
To ensure products with a "Private" status never appear in any front-end search or listing, use this code. This is a more comprehensive approach for highly sensitive products.
function exclude_private_products( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Check if it's a search or any other front-end product query
if ( $query->is_search() || ( isset( $query->query_vars['post_type'] ) && $query->query_vars['post_type'] === 'product' ) ) {
$post_status = $query->get( 'post_status' );
// If post_status is not explicitly set, set it to only public statuses
if ( empty( $post_status ) ) {
$query->set( 'post_status', 'publish' );
}
}
}
}
add_action( 'pre_get_posts', 'exclude_private_products' );
Important Considerations Before Proceeding
- Backup Your Site: Always create a full backup of your website before adding custom code.
- Use a Child Theme: Adding code directly to your theme's
functions.phpfile is not recommended, as updates will erase your changes. Use a child theme or a management plugin. - Test Thoroughly: After implementing any code, test your site's search functionality extensively to ensure it works as expected and doesn't break other features.
- Plugin Conflicts: If the problem persists after adding the code, a conflict with another plugin or your theme might be overriding the query. Try disabling other plugins temporarily to test.
While the WooCommerce team is aware of this discrepancy between product visibility and search behavior, a core fix has not yet been implemented. The custom code provided above serves as a reliable and widely-used workaround for store owners needing to control their product visibility completely.
Related Support Threads Support
-
How can I get product by “meta_data” using API GET requesthttps://wordpress.org/support/topic/hoh-can-i-get-product-by-meta_data/
-
Product missing Merchant listingshttps://wordpress.org/support/topic/product-missing-merchant-listings/
-
Critical Issue: Product & Merchant Listings Schema in Google Rich Testhttps://wordpress.org/support/topic/critical-issue-product-merchant-listings-schema-in-google-rich-test/
-
GTIN / UPC Search on Productshttps://wordpress.org/support/topic/gtin-upc-search-on-products/
-
Hide hidden products from WP site searchhttps://wordpress.org/support/topic/hide-hidden-products-from-wp-site-search/
-
Sepethttps://wordpress.org/support/topic/sepet/
-
Product Snippets in Merchant Center & Search Consolehttps://wordpress.org/support/topic/product-snippets-in-merchant-center-search-console/
-
Product Snippet on Landing Pagehttps://wordpress.org/support/topic/product-snippet-on-landing-page/
-
What is Catalogue visibility: Hidden supposed to mean?https://wordpress.org/support/topic/what-is-catalogue-visibility-hidden-supposed-to-mean/
-
Searching All Products on Dashboardhttps://wordpress.org/support/topic/searching-all-products-on-dashboard/
-
WooComerce Shortcode for Query based on Search Termhttps://wordpress.org/support/topic/woocomerce-shortcode-for-query-based-on-search-term/
-
Private Products Appear in Advanced Search Resultshttps://wordpress.org/support/topic/private-products-appear-in-advanced-search-results/