February 27, 2014 / IST / How-to Guides, Web Development.

WordPress is a wonderful Blog or CMS. Here you can post your creation selecting category and tag. You can also search to display appropriate related post result. But native wordpress search technique can increase CPU usage of hosting server to 99 present to 100 present. Often i have seen due to increase of CPU usage, your hosting bans some necessary  server support and resource. Server puts cap on http resource and Mysql query.  When you search a term, wordpress excavates database top to bottom to find search result. It causes more CPU usage. On the place of entire search user wants to search a term in a particular but default wordpress doesn’t has this feature. Indeed, you have to install some plugins.

Let’s i share some tips to design a unique search form.

Step 1: You can see in searchform.php or header.php your simple form code will be-

<?php $search_text = get_search_query(); ?> 
<form method="get" id="searchform" action="<?php echo esc_url(home_url( '/' )); ?>/">
	<input type="text" value="<?php echo $search_text; ?>" name="s" id="s" />
	<input type="submit" id="searchsubmit" value="Search" />
</form>

Step 2: Add a dropdown box inside form for category name and its value. Let’s know that when you submit than form add ‘s’ variable with value. Understand this search url:

http://www.pointerbrain.com/?s=wordpress

“s” parameter query forces wordpress to display search result containing search terms. For search in a particular category only add “cat” parameter query with category id. Example: category id is 4 for How-to Guides

http://www.pointerbrain.com/?s=wordpress&cat=4

Search Form with category dropdown option:

wordpress-search

This is done simply by adding the function wp_dropdown_categories() into the search form. All you need to do is put the following code into searchform.php file of your theme (this is the file where search form code is stored by default, but not always necessary):

<?php $search_text = get_search_query(); ?> 
<form method="get" id="searchform" action="<?php echo esc_url(home_url( '/' )); ?>/">
	<input type="text" value="<?php echo $search_text; ?>" name="s" id="s" />
	<?php wp_dropdown_categories('show_count=1&hierarchical=1'); ?>
	<input type="submit" id="searchsubmit" value="Search" />
</form>

If your theme doesn’t have the searchform.php file, try to find the code of search form in header.php file (or sidebar.php, it depends on the location of search form in your blog) and then replace it with the code above.

Now you can refresh your blog and try some searchs. Don’t forget to choose category to narrow the search results. You’ll see the search by category functionality works well as expected.

The search by category functionality is very useful for some types of WordPress blog like a download website or an e-commerce website, which all the content are well-arranged in its category. This feature is helpful for users because they’ll find what they need faster and more accurately. Implementing search by category is a good way to improve user experience in your website.

I hope this tutorial is helpful for you. If you have any question, please leave in the comments.

  • Atul Kumar Singh

    Sir please tell me if I want serach result by tags and categories

    View Comment