Completely hide WooCommerce products from shop, product search and WordPress search
You can hide a WooCommerce product from the shop page and product search by choosing ‘Hidden’ in the ‘Catalog visibility’ options from the Publish metabox on a product page in the backend.
This is good and it hides the product as advertised from the shop and product search results but the product is still visible if searched from the regular WordPress search!
WordPress Search Results
So if you were to search your site with a dynamic URL parameter https://mydomain.com/?s=Hidden+Woo+Product
the product you had hidden would show in the search results.
add_action('pre_get_posts', 'wpse_187444_search_query_pre'); /** * Hide Catalog Products Only In Search * @link https://wordpress.stackexchange.com/questions/283393/hidden-woocommerce-products-still-showing-up-in-search-results/283397 * @since 1.7.0 */ function wpse_187444_search_query_pre($query) { if ($query->is_search() && $query->is_main_query()) { $tax_query = $query->get('tax_query', array()); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'exclude-from-catalog', 'operator' => 'NOT IN', ); $query->set('tax_query', $tax_query); } }
So in the above code snippet (original source in the link value) a pre get posts filter is run on the main search query and any products which have Catalog Hidden set are not returned to the search results, add the code in your themes functions.php file.
Google Index
Another issue is that the hidden products are still index by Google and visible via an site Google search or sitemap. You have a couple of options to solve this issue.
- You can set each product manually to noindex/nofollow in your SEO plugin per page
- If you have many products to hide you can assign a category to the products and run a filter to exclude those products from being indexed. This example uses WordPress SEO – Yoast to set noindex.
Always recheck your sitemap or run a Google site search to check for hidden product content site:mydomain.com Hidden Woo Product
Thanks, exactly what I was looking for.
Thanks for this function! It works for normal WP searches, but it’s important to point out that sometimes themes include their own search functions, often when including AJAX features.
They might respect 99% of the WordPress search configuration and functions, but that 1% will be overridden in their search’s code.
For instance, I use Woodmart theme. It has a decent AJAX search built in. However, it doesn’t care one iota about the function shared above or any other search settings I apply which seek to hide a specific product.
Just wanted to point that out. Thanks again!
Thanks! Your solution worked for me!
Thanks for the article. Is it possible to query ‘hidden’ products? I have a case where I need to list products that are hidden (aka discontinued products). I don’t want these to show up in the catalog, but I do want to show them on occasion with a custom query. Thanks