WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
  • Deals
  • Glossary
  • Videos
  • Products
X
☰
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

WPBeginner» Blog» Tutorials» How to Use Multiple Search Forms In WordPress

How to Use Multiple Search Forms In WordPress

Last updated on June 22nd, 2012 by Editorial Staff
24 Shares
Share
Tweet
Share
Pin
Special WordPress Hosting offer for WPBeginner Readers
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.

24 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

15 Comments

Leave a Reply
  1. Pablo says:
    Jan 14, 2019 at 8:19 pm

    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.

    Reply
    • WPBeginner Support says:
      Jan 15, 2019 at 2:17 pm

      For one option you could add placeholder = “text” in the input tag for which you want to display each text

      Reply
  2. Jude Martenstyn says:
    Aug 10, 2017 at 1:14 pm

    Perfectly works. Thank you.

    Reply
  3. Danny says:
    Jun 13, 2016 at 8:17 am

    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?

    Reply
    • Sofi smith says:
      May 23, 2017 at 7:47 am

      Hi Danny,

      Did you get this to work? The / is depreciated – use in the actual form rendering instead and it works :)

      Hope this helps.

      Reply
      • Jim Bouschor says:
        Apr 18, 2018 at 10:36 am

        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…

        Reply
  4. Zaheer Abbas says:
    Jan 11, 2016 at 3:50 am

    Great Work.

    Reply
  5. Daniel Gadd says:
    Nov 14, 2015 at 10:51 am

    Did this get resolved.Does it work.I hope so as im investing time here now with this post

    Reply
  6. sunnyrainyareallgood says:
    Sep 23, 2015 at 12:36 am

    Is it ok to name template as search-normal.php and search-book.php instead of normal-search.php and book-search.php?

    Reply
  7. tourismpics says:
    May 12, 2014 at 9:12 pm

    This doesn’t appear to work. Any ideas?

    Reply
  8. Arifur Rahman says:
    Jan 23, 2014 at 11:56 pm

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

    Reply
  9. Anthony says:
    Dec 12, 2012 at 11:41 am

    What does the line

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

    Do?

    Reply
  10. Jenny says:
    Dec 8, 2012 at 2:28 pm

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

    Reply
  11. EvertVd says:
    Nov 4, 2012 at 8:01 am

    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.

    Reply
  12. Mirza Ali HuXain Qazilbash says:
    Oct 20, 2011 at 4:17 pm

    useful stuff for beginner..

    Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
WPForms Logo
WPForms
Drag & Drop WordPress Form Builder Plugin. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 25 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2019 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2019)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2019)
    • SiteGround Reviews from 1032 Users & Our Experts (2019)
    • Bluehost Review from Real Users + Performance Stats (2019)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Start a Podcast with WordPress (Step by Step)
    • How to Choose the Best Domain Name (8 Tips and Tools)
    • How to Setup a Professional Email Address with Google Apps and Gmail
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2019 – Step by Step Guide
Deals & Coupons (view all)
Dreamhost
DreamHost Coupon
Get 40% OFF on DreamHost and get a Free Domain.
Elegant Themes
Elegant Themes Deal
Get all 87 amazingly beautiful WordPress themes by Elegant Themes for only $69. That is like $0.79 per theme!
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • Awesome Motive

Copyright © 2009 - 2019 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

WordPress hosting by HostGator | WordPress CDN by MaxCDN | WordPress Security by Sucuri.