This tutorial demonstrates how to use separate widgets for product searches and blog searches, and how to exclude WooCommerce products from the search results in WordPress.
Why Hide WooCommerce Products from the WordPress Search Results?
Here is the scenario: Your WordPress e-commerce website has products, blog posts, and pages. You want to limit the search results to either products or blog posts depending on where the search is initiated (i.e. which search widget is used).
Unfortunately, the WordPress search widget displays both the products and blog posts on the search results page. This might be confusing to a customer when searching for a product and getting blog posts mixed into the search results.
What’s the Difference Between the WordPress & WooCommerce Search Widgets?
WordPress and WooCommerce offer their own search widgets out of the box. Both are separate and function independently of each other.
Here is the primary difference:
- The WooCommerce search widget will return products matching the search criteria. This means that products will only be displayed in the search results.
- The WordPress search widget will return results for all post types, including products. It will search through every post type and output results for posts, pages, products, etc.
How to Exclude WooCommerce Products from the Search Results in WordPress
The solution to determining the post types that will be allowed in the search results is to create a function that hooks into pre_get_posts.
Because the WooCommerce search widget is already set up to output only products, the query for the WordPress search widget is what needs to be modified.
The following steps will walk you through how to separate the search results.
Step 1
Add the WooCommerce product search widget to the shop sidebar. Depending on how the website is set up, it may be listed as the default sidebar.
Step 2
Add the WordPress search widget to the blog sidebar.
TIP: You may want to refer to your theme’s documentation for help with steps 1 & 2.
Step 3
Copy and paste the following PHP code into your theme’s functions.php file:
TIP: You can control which post types get removed from the query on line 23. The product post type and page post type are used in this tutorial.
Final Thoughts
The Search Exclude plugin is available for free in the WordPress repository that will allow you to remove specific post types from the WordPress search results.
If you are unsure about the name of the post type to remove, you may need to refer to your theme’s documentation for a list of custom post types.
You can also discover the names of all registered post types by using the WordPress get_post_types function.
For more tips and suggestions, take a look at the articles How to Bulk Edit WooCommerce Products in WordPress, How to Show Woocommerce Related Products by Name, and How to Create Smart Coupons in Woocommerce.
Need help with customization and development? Our WordPress Development Service is a complete solution for new or existing WordPress websites.
Hey awesome tutorial, I came across an issue where my WooCommerce product search results in ‘page not found’ although the autosuggestions appear. The URL is: https://modernpinkpaper.com/?s=hearts&post_type=product
When I change it to https://modernpinkpaper.com/?s=hearts&post_type=product_cat I get results but they appear as blog post. Do you know how I can fix this?
Hi Zubair, it could possibly be caused by a number of things:
1. Check the WordPress Rewrite Rules using the Rewrite Rules Inspector plugin:
https://wordpress.org/plugins/rewrite-rules-inspector/
2. Try switching the theme to see if the same problem exists.
3. Disable plugins one at a time to determine if any of them may be causing the redirect issue. You should also clear transient data from the database:
https://wordpress.org/plugins/transients-manager/
I hope these suggestions help!
Works like charm! Thanks for your help!
Thanks!
With my theme there is another issue: When I type in and enter a searchstring in the WordPress search field the placeholder text of the woocommerce search field is replaced by the searchstring, too. Any suggestions?
Thanks for your share.
Works perfectly on my site! Thanks a lot for sharing! 🙂
Hello, I wanna really thank you for this. You saved me!! Even the plugin you mentioned doesn’t work, but your code does!!!!
Awesome code!