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 Create Advanced Search Form in WordPress for Custom Post Types

Last updated on by
Follow WPBeginner on YouTube
How to Create Advanced Search Form in WordPress for Custom Post Types

Have you ever worked on a WordPress site that has a lot of custom post types? Well as-is WordPress search is a disaster which is why many bloggers use Google custom search. Well, we were working on a site that has a good amount of resource articles, videos, books, and blog posts. Using the default WordPress search just wasn’t cutting it. Using Google Search also was not a viable option. Therefore, we decided to create an advanced search form which lets user pick which area of the site they want to search by limiting custom post types via checkboxes. Users can combine their search queries and most importantly, we made it so the checkboxes are saved along with the search query. This lets the user sees exactly what they searched for, and they can modify the parameters. In this article, we will show you how to create an advanced search form in WordPress utilizing the power of the WordPress search query and limiting the results by custom post types.

First thing you need to do is open your searchform.php file or wherever your search form is stored. Then add the following fields inside the form code:

<input type="hidden" name="post_type[]" value="articles" />
<input type="hidden" name="post_type[]" value="post" />
<input type="hidden" name="post_type[]" value="videos" /> 
<input type="hidden" name="post_type[]" value="books" />  

Don’t forget to replace the value with your custom post types. The code above basically limits your search results to those post types. Well, if you noticed we pretty much added all post types available except for pages. Well, there is a good reason for doing so which we will get to later. So make sure to include ALL post types that you want to search for using the main search button. These fields are hidden, so the user doesn’t see these.

Next open your search.php file and paste the following codes above your loop content, so your users can see the options at the top.

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" <?php if(is_search()) { ?>value="<?php the_search_query(); ?>" <?php } else { ?>value="Enter keywords &hellip;" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"<?php } ?> /><br />
	
<?php $query_types = get_query_var('post_type'); ?>
    
<input type="checkbox" name="post_type[]" value="articles" <?php if (in_array('articles', $query_types)) { echo 'checked="checked"'; } ?> /><label>Articles</label>
<input type="checkbox" name="post_type[]" value="post" <?php if (in_array('post', $query_types)) { echo 'checked="checked"'; } ?> /><label>Blog</label>
<input type="checkbox" name="post_type[]" value="books" <?php if (in_array('books', $query_types)) { echo 'checked="checked"'; } ?> /><label>Books</label>
<input type="checkbox" name="post_type[]" value="videos" <?php if (in_array('videos', $query_types)) { echo 'checked="checked"'; } ?> /><label>Videos</label>
    
<input type="submit" id="searchsubmit" value="Search" />
</form>

This will add a search box above your results with the search query inside the input box. This will also check which post types are being searched for in the query, and make the appropriate checkboxes checked. Remember, how we added all post types in the hidden field. Well, we added it just so we can run the in_array check and keep the checkboxes checked. There was no documentation on how to do this otherwise, so this was the best way we found that does the job. Below is a preview of how the search box looks:

Advanced Search Form

From there, the user can simply modify the parameter as they please.

Hopefully this article helped those in need. When we are doing the research, there were bunch of incomplete articles. Found the answer of adding the checkboxes in the WordPress support forum in an older thread however they were only talking about hidden fields. Whereas we wanted to give users the option to modify the search query. Props to @tammyhart for pointing us in the right way to do the checked query for the variable.


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 »
  • mayur

    Sir.. can you plz attach screenshot with tutorials afterwords?? i think it’s more helpful for us..

  • Sean

    Check out WP Advanced Search: http://wpadvancedsearch.com/

    We’ve got support for taxonomies, custom fields, and just about everything else that WP_Query supports.

  • Brad

    How could I use this to search a custom table of data. This is for a sports team site, and basically, I need a search box where someone can “Search by”: Last name, First name, or Team.

    So how can I search by a field name?

  • Darren

    This is an awesome tutorial, thank you.

    Is there a way that I could mix this with categories? So that I could have both custom post types and categories selectable?

    Thanks again :).

  • ONEEEZY

    Dang.. still nothing. . .

  • Marie

    Hi, there isn’t a way to tell the engine to automatically list all terms or tags or custom field? thanks

    • http://www.wpbeginner.com Editorial Staff

      Not yet.

  • Paul

    This is an aspect of WordPress that could be improved, I agree.
    If you don’t want to code, you can try Pippin’s search plugin:
    http://pippinsplugins.com/advanced-search-shortcode/

  • Elliott

    Nice tutorial… Am designing a custom search for my new site at the moment.

    Curious why you don’t think searching on pages is a good idea?

    • http://www.wpbeginner.com Editorial Staff

      You can search pages except in most cases your about page, contact page, legal page, privacy policy page are mostly irrelevant.

  • Fakeuser

    I’m getting this error after changing the name of the input fields to s[]

    Warning: stripslashes() expects parameter 1 to be string, array given in wp-includes\query.php on line 2179

    I’ve changed the call to stripslashes() call to stripslashes_deep() but it broke other thigs… any sugestions?

  • Benjamin Wilde

    I’m looking for a way to have a form with separate controls for location AND category, both in dropdown boxes. Obviously I can do this, but I have no idea how to do a multiple-query search in WordPress. Any ideas?

  • Aldo Valverde

    Do you think is it possible to do the same but including Post Type Categories as well….

    …I mean, the same Post Type checkboxes and when user select an specific checkbox that appear-down other checkboxes but as categories and filter even more the Search of WordPress ?

  • http://www.facebook.com/profile.php?id=1059076335 Rocky Ouprasith

    Awesome! thanks for the help!
    there is one thing though…

    my post types are all styled differently.
    how can I assign the template of a post type with the correct post types?

    thanks!

    • http://www.wpbeginner.com Editorial Staff

      Use CSS classes. Each post outputs its own post classes i.e categories, post types etc.

  • http://www.facebook.com/profile.php?id=816736736 Merianos Nikos

    Very nice article :)

  • SDesign

    Great tutorial! One question though: What would the syntax be for making the checkboxes in the form use taxonomies rather than post types?

    Would something like this work?:

    Perhaps this is worthy of another tutorial. Any additional help is greatly appreciated.

    • Thad

      Ever come across anything that explains this?

      • SDESIGN

        No, I decided to go with custom post types instead.

        If anyone has any insight, on this it would be greatly appreciated.

        The specific question is how to make something like this work using taxonomies instead of post types?

  • CathieHeart

    Hi, you wouldn’t happen to also have a section to add maybe for those of us wanting to a dropdown menu search form interface rather than checkboxes? 
    Thanks.

    • http://www.wpbeginner.com/ wpbeginner

       @CathieHeart That can be accomplished using the similar technique.

  • mainaPlus

    Does this work with a buddypress enabled site?

    • http://www.wpbeginner.com/ wpbeginner

       @mainaPlus Haven’t tried it with BuddyPress so not sure

  • http://none.com/ Robin

    Would this be able to work with a buddypress enabled site?

    • http://www.wpbeginner.com Editorial Staff

      Not sure about that. We don’t have much experience with BuddyPress.

    • http://www.wpbeginner.com/ wpbeginner

      Haven’t tried it with BuddyPress so not sure.

  • http://www.feed2need.com/ Abdelhadi Touil

    Very nice tutorial! Thanks.

  • http://www.i95dev.com/ Herny Louis

    Thanks Syed and David. Need to test it on one of my websites. Definitely this kind of categorization will help visitors to do narrow search.

  • zachshallbetter1

    I would love to see a way to do this with custom fields as well.

    • oneeezy

      Yes!
      What Zach said…

      It would be ideal to have this “Advanced Form” be able to search through custom fields. You could make the ultimate search form this way. Any takers??? C’mon WP Beginner!!