Family Best Time >> Electronics

How to use multiple search forms in WordPress

We recently showed you how to limit search results for specific post types in WordPress. Now we are going to show you how you can create different/multiple search forms entirely. This way, each form can be limited to searching for a specific post type. Although this is not very difficult, it will require you to have a basic understanding of WordPress templates.

First, you'll need some search forms. Place the following code where you want them to be on your blog:

 

To specify what type of search this form will do, simply change the value of the hidden field. Right now it's set to "normal", but it can be anything you want. Next, we need to modify the search.php file. Open it and replace everything in it with this code (copy the existing code to your clipboard first, you'll need it in a minute):

 

We'll assume you have two search forms, normal and books. This code is simply redirecting the lookup to the php file that handles that specific query. Now we just have to create those files. So go ahead and create a file normal-search.php and books-search.php (just replace "normal" and "books" with whatever value you've been using).

Now, in normal-search.php, copy and paste the following code:

 $ args = array ('post_type' => 'post'); $ args = array_merge ($ args, $ wp_query-> query); query_posts ($ args); 

Immediately after this, paste the clipboard loop code that you copied from the search.php file. Together, this code will search your normal blog posts only. Now, in the books-search.php file, add this bit of code and paste the loop right after:

 $ args = array ('post_type' => 'books'); $ args = array_merge ($ args, $ wp_query-> query); query_posts ($ args); 

This will cause WordPress to look only for the "books" custom post type. You can repeat this process for as many search forms as you like.