Recently we 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 altogether. This way each form can be limited to searching for a specific post type. Although this isn’t very hard it will require you to have a basic understanding of WordPress templates.
First, you will need some search forms. Place the following code wherever you want them to be in your blog:
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/"> <input type="text" value="" name="s" id="s" /> <input type="hidden" name="search-type" value="normal" /> <input name="submit" type="submit" value="Go" /> </form>
In order to specify what sort of search this form will be doing, just change the value of the hidden field. Right now it is set to “normal” but it can be anything you want. Next, we need to modify the search.php file. Open it up and replace everything in it with this code (copy the existing code to your clipboard first, you will need it in a minute):
<?php if(isset($_GET['search-type'])) { $type = $_GET['search-type']; if($type == 'random') { load_template(TEMPLATEPATH . '/normal-search.php'); } elseif($type == 'books') { load_template(TEMPLATEPATH . '/books-search.php'); } } ?>
So we will be assuming that you have two search forms, normal and books. This code is simply redirecting the search to the php file that handles that specific query. Now we just have to create those files. So, go ahead and create a normal-search.php and books-search.php file (just replace “normal” and “books” with whatever values you have 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 loop code from your clipboard that you copied from the search.php file. Together, this code will search only your normal blog posts. Now, in the books-search.php file add this bit of code and again paste the loop right after it:
$args = array( 'post_type' => 'books' ); $args = array_merge( $args, $wp_query->query ); query_posts( $args );
This will cause WordPress to search only for the custom post type of “books”. You can repeat this process for as many search forms as you would like.
And how would you change the placehoder text for each form?
For instance, placehoder text for form 1: Search the normal blog. Placeholder text fro form 2: Search books for form 2.
For one option you could add placeholder = “text” in the input tag for which you want to display each text
Perfectly works. Thank you.
I cannot get this to work, search results come up with a blank page
Anyone else get this working or know of another solution for creating multiple forms?
Hi Danny,
Did you get this to work? The / is depreciated – use in the actual form rendering instead and it works
Hope this helps.
i followed this exactly and i’m able to load the proper search results templates when i perform a search, but NO results appear. Also when I do a search in one of my forms, the other form has the searched term in the input field when i return to it.
If anyone can shed some light it would be greatly appreciated. I cannot find anything useful on the internet somehow…
Great Work.
Did this get resolved.Does it work.I hope so as im investing time here now with this post
Is it ok to name template as search-normal.php and search-book.php instead of normal-search.php and book-search.php?
This doesn’t appear to work. Any ideas?
dear admin, I don’t know why I could not get any effective result by following this tutorial?
What does the line
“$args = array_merge( $args, $wp_query->query );”
Do?
…think you’ve over-coded your search box,did anyone tell you it doesn’t work at all?
There is small problem with this: If you place two search forms on a page you have two input-boxes with the same id (id=’s’). Which at the very least will not validate.
useful stuff for beginner..