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

How to Create Advanced Search Form in WordPress for Custom Post Types

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
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" />

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 experts 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 »


  1. vijay says:

    Thanks for the information, I want to know whether there is any way I can have dropdown style of search forms, as I have large number of post types, it would make it look ugly if I put all of them below search bar.

  2. Penguin says:

    Hi, Thanks a lot. This works pretty good for me. I am using dropdown instead of checkbox. But how can I style it to make it smilar to the one in LinkedIn header search section? Does that requires javascript? Do I use font icons for that? I am using fontawesome in my header. But I don’t know how to integrate this with fontawesome in a dropdown.

  3. mayur says:

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

  4. Sean says:

    Check out WP Advanced Search:

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

  5. Brad says:

    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?

  6. Darren says:

    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 :).

  7. ONEEEZY says:

    Dang.. still nothing. . .

  8. Marie says:

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

  9. Paul says:

    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:

  10. Elliott says:

    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?

    • Editorial Staff says:

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

  11. Fakeuser says:

    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?

  12. Benjamin Wilde says:

    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?

  13. Aldo Valverde says:

    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 ?

  14. Rocky Ouprasith says:

    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?


    • Editorial Staff says:

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

  15. Merianos Nikos says:

    Very nice article :)

  16. SDesign says:

    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 says:

      Ever come across anything that explains this?

      • SDESIGN says:

        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?

  17. CathieHeart says:

    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? 

  18. mainaPlus says:

    Does this work with a buddypress enabled site?

  19. Robin says:

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

  20. Abdelhadi Touil says:

    Very nice tutorial! Thanks.

  21. Herny Louis says:

    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.

  22. zachshallbetter1 says:

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

    • oneeezy says:

      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!!

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.