Using Multiple Search Forms In WordPress
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:
1 2 3 4 5 |
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/"> <input type="text" value="" name="s" id="s" /> <input type="hidden" name="search-type" value="blog" /> <input name="submit" type="submit" value="Go" /> </form> |
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.
1 2 3 4 5 6 7 8 9 10 |
<?php if(isset($_GET['search-type'])) { $type = $_GET['search-type']; if($type == 'blog') { load_template(TEMPLATEPATH . '/blog-search.php'); } elseif($type == 'products') { load_template(TEMPLATEPATH . '/products-search.php'); } } ?> |
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:
1 2 3 |
$args = array( 'post_type' => 'post' ); $args = array_merge( $args, $wp_query->query ); query_posts( $args ); |
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:
1 2 3 |
$args = array( 'post_type' => 'products' ); $args = array_merge( $args, $wp_query->query ); query_posts( $args ); |
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.
23 Comments
Maor Barazany
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 …
Paul Robinson
Hi Maor,
Yep that is a great way to do it. Thanks for sharing.
nomadone
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
Ivan Perez
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:
http://gyazo.com/56db7f4603d2395a41619de818425ce0.png
Can you explain how to fix this issue?
Thanks!
Ivan Perez
Oops, wrong pic
a correct pic:
http://gyazo.com/b328b525a747c40a1edf1ea6e54c4dab.png
Thanks!
Paul Robinson
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.
Ivan Perez
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!
Paul Robinson
I’m not sure what to say to that… 😆
I’m glad you seem to have fixed the problem though.
Qwango
Nicely put, exactly what I needed… and also thanks to Ivan, his reply actually sorted me out! 🙂
Paul Robinson
Glad the post, and Ivan’s comment helped you Qwango. 🙂
Niraj
Just wanted to share, if anybody looking for multiple category search then this tutorial will help you a lot
http://webstutorial.com/wordpress-multiple-category-search/content-management-system-cms/wordpress-cms
Paul Robinson
Thanks for sharing that handy little tutorial Niraj. 🙂
sunil
If I’m using this solution, how can I also have a search function that returns results regardless of post type?
Paul Robinson
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).
Agnes
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?
Paul Robinson
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
.Agnes
Sorry I meant for the above function
Paul Robinson
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.
berri
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” />
Kim
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.
Paul Robinson
Hi Kim,
Nicely done. 🙂
get_query_var()
wouldn’t really help you here as it grabs variables stored inWP_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 wayget_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()
andadd_rewrite_rule()
.Rewrite rules are painful to get used to, but they are extraordinarily powerful. 🙂