How to change Default WordPress search to product search

If you want to make the default WordPress search to product search, then I am writing this post only for you. Just now I was building an e-commerce website, and the same issue happened to me in the Astra theme. I googled a lot but couldn’t find the correct answer. Almost the whole day passed, so I thought that if you ever faced such an issue, you should get the answer quickly, so I would like to share it with you.

If you have an e-commerce website and the search page products are shown on a separate page in the post type, then it will not be fun, right? Show the search results on the shop page. It would be correct if the search results came in the same manner as our shop page, and it also looked good.

Also Read: Web Development Company Kandivali, Mumbai

There are two types of searches in WordPress, the default WordPress search to product search. If your site is an e-commerce site, then you will get two types of searches, and if it is not an e-commerce site, then you will get one type, which is a post-type search.

1. Post type search

Post Type Search is the default search in WordPress. This search will search products, pages, and posts and show all the post types on the page. There are many ways to make the post type default to search-only products. Now you must be thinking that when the search for users is available, which is product type, then what is the need to do a post-type search? Because there are a lot of default post types and default searches in the pre-template theme it has a very good design. like it is in the Astra theme template.

Default WordPress search to product search astra theme

If you run an online store, consider limiting the search to only WooCommerce products. This can enhance the user experience by allowing them to find products faster, which can lead to an increased conversion rate on your website. to change the default WordPress search to product search, do things as per below.

Looking for a website developer? Contact us today!

To add this filter to your Astra theme’s functions.php file, simply add the following code to change the default WordPress search to product search:

add_filter( 'get_search_form', 'astra_get_search_form_callback' );
function astra_get_search_form_callback( $search_form ) {
	$search_form = str_replace( '-1">', '-1"/><input type="hidden" name="post_type" value="product"/>', $search_form ); 
	return $search_form;
}

Ways to add this code, as per the snapshot below

Default WordPress search to product search

In this way, you can change the default WordPress search to product search in the any theme. By doing this, your search result page will also look like a shop page with the same product type. If you like the search page of WordPress or you want to display empty products in the search result, then use the given code in function.php.

function wp_search_filter($query) {
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    if ( (strpos($url,'post_type=product') !== false) && is_search() ) {
        $query->set('post_type', 'product');
    }
    return $query;
}

add_filter('pre_get_posts','wp_search_filter');

If you are searching for the product type of WordPress woo-commerce, then there will be no issue, the search result will be shown to you on the shop page only. It will be shown in the same design as your shop page because it is for products. You don’t even need to do anything. If you can use it or install it on the website, then you can use the above solution.

More solutions can be found here: wpmagictricks.com

I hope this article will help you solve your problem. If you need some help from me, you can comment or contact me. If you have any other issues, you can also contact me. I will take the time to write an article to solve your problem or help you directly.

Leave a Comment