Using Multiple Search Forms In WordPress

/ Wordpress / by Paul Robinson / 23 Comments
This post was published back on October 24, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

Luichy asked this:

Im using wordpress as CMS, so i need one search form just to find in the blog section and another search form just to find for the custom post type “products”…

This is perfectly possible, but more difficult than you would first think. Let’s take a look at how to get it all working.

The Search Forms

First off we need two (or more) search forms. Place them in whatever locations you wish. They should look a little like this:

The only thing that really needs to change for each form is the value of the hidden field ‘search-type’. You will need to change it to a name that identifies what you want to search. It’s only for your own reference so it doesn’t have to match anything although it would help. So in Luichy’s case it could be ‘blog’ and ‘products’.

Modifying The Search Template

Next is to modify your search template. You will need to open the file called search.php. This is where you need a little bit of knowledge of WordPress’ templates. We are going to empty the search.php file, before we do that though you may want to copy the code inside it and keep it in notepad (or another text editor) if you aren’t very comfortable recreating the loop again.

Okay. See how you are detecting the type of search by grabbing the hidden value sent via the search form? We use that to detect which search is being made & then load a specific page template. So you will need change the words ‘blog’ & ‘products’ throughout to whatever your search types are. Also if you created more than two search forms earlier you can add more by adding extra elseif sections to it.

Creating The New Search Templates

Now you will need to create your new search template pages. In Luichy’s case these would be blog-search.php & products-search.php. In the file paste the code back from that notepad you kept, or re-write the loop to your satisfaction. Once that’s done you will need to modify the post query to limit the results to your selected product type or category. That’s up to you, but as this tutorial was made for Luichy I’m going to stick with what was asked. The blog & product custom post type.

In blog-search.php add this before the loop:

That will add your instructions to only search the normal blog posts & not the custom post types or pages while maintaining the normal search query.

In products-search.php add this before the loop:

That will do the same as the previous query except it will only search the custom post type named ‘products’.

That’s all there is to it. Unfortunately it requires at least a little knowledge of WordPress templates to do, unless I go into extreme detail, but if you have anything you aren’t sure about or you have some questions let me know via the comments & I’ll try my best to help out. Thanks for reading.


Author’s gravatar

Very nice idea.

I think it may be done better, without needing to duplicate search files.
You may have something like this, all in the search.php file

Then the only thing is to put inside the hidden field’s value, the name of the post_type to search, and then you may have a lot of post_types and you won’t have to recreate a special search-{type}.php for each …

Author’s gravatar author

Hi Maor,

Yep that is a great way to do it. Thanks for sharing.

Author’s gravatar

Wow thanks a ton, I tried using the templates method to include various loops for each post type but somehow it never worked. When I tried Maor’s method it worked fine, thanks to both of you for sharing this great piece of code.

I’ve searched for ages but nothing else out there seemed to do the trick

Author’s gravatar

Hello, this tutorial was worked very well, now i have a problem that archive box shows appear “No results” in last footer, it’s from search.php, not template-search.php, look this pic:

Can you explain how to fix this issue?


Author’s gravatar author

Hi Ivan,

If I understand from the image correctly, your search.php file is showing underneath your footer. The only thing I can think that would cause that is that it is actually being included a second time somewhere else in your theme and because you have altered search.php to allow my tutorial to work it is now showing incorrectly.

The only advice I can give is to look through your footer.php file to see if the search.php file is included in your template somewhere.

Hope that helps.

Author’s gravatar

Hey, thanks for reply…

I looked trough my footer and there no search.php is included and oh yes, my search.php is altered for some reason, do you want a snippet here?

I think i should to create like this in search.php without HTML tags and loop for default search template:

mainsearch.php is based from search.php template without this snippet above.

I have no idea which $type == ” for random words, i tested with random words and (goes to edit this snippet and saved on search.php, testing now…)… WOW, it worked!

This problem is resolved now

Thanks a lot!

Author’s gravatar author

I’m not sure what to say to that… 😆

I’m glad you seem to have fixed the problem though.

Author’s gravatar

Nicely put, exactly what I needed… and also thanks to Ivan, his reply actually sorted me out! 🙂

Author’s gravatar author

Glad the post, and Ivan’s comment helped you Qwango. 🙂

Author’s gravatar

If I’m using this solution, how can I also have a search function that returns results regardless of post type?

Author’s gravatar author

Hi Sunil,

in one of the search templates queries you would just leave out the query completely & it would default to the standard WP query, which is to search everything (as far as I’m aware).

Author’s gravatar

I am using the same check to send to either a search-objects.php (for post-type objects) or a search-all.php (for all) and it works fine.
However, I would also like to display some widgets, with widget-logic, depending if it the search is for this object post-type.
I try using the conditional tage “is_page_template (search-objects.php)” but it does not work, may be if that tag is only for page templates (and this is not a page)
I also try create a function in my functions file:

and then using is_objects_search() but it also does not seem to work.
I also tried set that as a page using that template under another page (tne I could use a tag in widget logic for that page), but don’t know how to retain the strings then…
I am not a PHP expert and am wodnering then how to do! Any idea?

Author’s gravatar author

Hi Agnes,

You are right in thinking that is_page_template() is only for page templates. You code looks fine except for one mistake. The 4th line should check the value of $is_objects_search not $is_post_type.

Author’s gravatar

Sorry I meant for the above function

Author’s gravatar author

Hmm. Not sure why that isn’t working if post_type is in the URL.

If you are loading two different search pages as I have in the tutorial, could you not just create two different sidebars to load? I agree that way is much more efficient, but it is something you could fall back on.

Author’s gravatar

Hi all,

How can I get right down the search icon, I am working with Catch Box theme and searchforms.php

This code is put Rigt top, is possible to move Right down?

<form method="get" id="searchform" action="/”>

<input type="text" value="” name=”s” id=”s” />

Author’s gravatar

This is great, thank you.

I needed to add a body class based on which search template was being loaded. I finally figured out how to do it, and so I’m adding it here in case it is helpful to someone else. The following code goes in your functions.php.

I realize get_query_var() would be more secure than $_GET, but I couldn’t get it to work. If you can, please share.

Author’s gravatar author

Hi Kim,

Nicely done. 🙂

get_query_var() wouldn’t really help you here as it grabs variables stored in WP_Query, not on the URL. Well technically it does, but only if the variable is registered with WordPress’ query vars.

I’ll try to add a new tutorial at some point, but you can get this multi seach form code to use fancy URLs (instead of query strings) and get it to store in WP_Query, that way get_query_var() would work in you code.

If you want to have a go before I make the tutorial, I can tell you that the code involves a combination of add_query_var(), get_query_var() and add_rewrite_rule().

Rewrite rules are painful to get used to, but they are extraordinarily powerful. 🙂

Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more