Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Use Multiple Search Forms In WordPress

Last updated on by
Elegant Themes
How to Use Multiple Search Forms In WordPress

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.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Arifur Rahman says:

    dear admin, I don’t know why I could not get any effective result by following this tutorial?

  2. Anthony says:

    What does the line

    “$args = array_merge( $args, $wp_query->query );”

    Do?

  3. Jenny says:

    …think you’ve over-coded your search box,did anyone tell you it doesn’t work at all?

  4. EvertVd says:

    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.

  5. Mirza Ali HuXain Qazilbash says:

    useful stuff for beginner..

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.