This article, ItsmeIT, will share how to fix the product not found error when searching on WordPress or displaying articles without any products found on your site.
How to fix the product not found error when searching on WordPress
There are some WordPress themes for blogs or news websites that only have a search function that displays articles without a product search function. The reason for this is that the code in the theme only allows the “post_type” to be set as “post”. If your blog site sells products other than posts, you can follow ItsmeIT to fix this problem.
Steps to add product search to the Wordpress Search Form
This is a simple way to customize your search form with just a few lines of code, allowing it to search for both post and product, or whatever you want. Follow these steps to fix WooCommerce single product page not working:
Open the functions.php file of the theme you are using. You can open it in any way you prefer, such as directly on your hosting/cPanel, using FileZilla, or opening it directly in the WordPress admin area by navigating to Appearance -> Theme Editor -> functions.php. Then, paste in the PHP code below:
Above, ItsmeIT used the “add_filter” hook to modify the content before displaying the search results when using the form. Additionally, I added a condition so that if the WooCommerce plugin is installed on the site, the modification only applies when searching with the form and fixes the issue of the product not found error when searching on Wordpress.
By default, the query only searches for posts using this code: $query->set('post_type', 'post');
This means that you can only search for posts. To enable searching for products, you simply need to add ‘product’ to the array. If you want the search results to display menus or categories as well, you can also add them to the same array (e.g. array('product', 'post', 'category')
).
Save the file and test the results.
Note: You need to back up the file before doing this, if there is any problem with the code, you can revert it back to the original.
Here, ItsmeIT has shared how to customize “post_type” in WordPress so that you can search for both articles and products on your website. If you have any better ways, feel free to share them in the comments section.
– Advertising –