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
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All 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 Create Advanced Search Form in WordPress for Custom Post Types

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

Last updated on March 1st, 2012 by Editorial Staff
74 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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.

74 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

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

    How to Start Your Own Podcast (Step by Step)

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

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

35 Comments

Leave a Reply
  1. tinhbeng says:
    Nov 3, 2016 at 9:59 pm

    Hi,
    How to me can add product post type in here?

    Thank,

    Reply
  2. vijay says:
    Apr 22, 2016 at 3:53 pm

    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.

    Reply
  3. Penguin says:
    Jul 14, 2015 at 6:08 am

    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.

    Reply
  4. mayur says:
    Feb 21, 2014 at 2:19 am

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

    Reply
  5. Sean says:
    Aug 3, 2013 at 11:44 am

    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.

    Reply
    • Thad says:
      Aug 28, 2013 at 11:04 am

      Thanks for sharing Sean, I also found this plugin that works fairly well for those not comfortable with diving into the PHP: http://wordpress.org/plugins/advance-wp-query-search-filter/

      Reply
  6. Brad says:
    Jun 27, 2013 at 3:32 pm

    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?

    Reply
  7. Darren says:
    Apr 22, 2013 at 7:58 pm

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

    Reply
    • Jonathan Stevens says:
      Jun 26, 2013 at 6:22 pm

      Hi Darren,

      I had the same problem. Ended up with this messy solution, but it worked:
      http://antimatterdesign.com/how-to-create-a-wordpress-advanced-search-form-from-scratch/

      Reply
  8. ONEEEZY says:
    Mar 21, 2013 at 4:26 pm

    Dang.. still nothing. . .

    Reply
  9. Marie says:
    Mar 15, 2013 at 8:12 pm

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

    Reply
    • Editorial Staff says:
      Mar 17, 2013 at 11:48 pm

      Not yet.

      Reply
  10. Paul says:
    Jan 1, 2013 at 7:06 am

    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/

    Reply
  11. Elliott says:
    Dec 5, 2012 at 4:23 pm

    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?

    Reply
    • Editorial Staff says:
      Dec 6, 2012 at 9:13 am

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

      Reply
  12. Fakeuser says:
    Nov 16, 2012 at 4:50 pm

    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?

    Reply
  13. Benjamin Wilde says:
    Nov 1, 2012 at 9:47 am

    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?

    Reply
  14. Aldo Valverde says:
    Oct 25, 2012 at 10:13 pm

    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 ?

    Reply
  15. Rocky Ouprasith says:
    Sep 12, 2012 at 6:04 pm

    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!

    Reply
    • Editorial Staff says:
      Sep 13, 2012 at 9:03 am

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

      Reply
  16. Merianos Nikos says:
    Sep 11, 2012 at 5:37 am

    Very nice article :)

    Reply
  17. SDesign says:
    Aug 22, 2012 at 1:37 am

    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.

    Reply
    • Thad says:
      Dec 5, 2012 at 10:02 am

      Ever come across anything that explains this?

      Reply
      • SDESIGN says:
        Dec 5, 2012 at 11:45 am

        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?

        Reply
  18. CathieHeart says:
    Mar 19, 2012 at 6:18 pm

    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.

    Reply
    • wpbeginner says:
      Mar 23, 2012 at 7:39 am

       @CathieHeart That can be accomplished using the similar technique.

      Reply
  19. mainaPlus says:
    Mar 7, 2012 at 7:20 pm

    Does this work with a buddypress enabled site?

    Reply
    • wpbeginner says:
      Mar 23, 2012 at 7:38 am

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

      Reply
  20. Robin says:
    Mar 7, 2012 at 3:26 am

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

    Reply
    • Editorial Staff says:
      Mar 12, 2012 at 7:57 am

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

      Reply
    • wpbeginner says:
      Mar 23, 2012 at 7:38 am

      Haven’t tried it with BuddyPress so not sure.

      Reply
  21. Abdelhadi Touil says:
    Mar 6, 2012 at 2:26 pm

    Very nice tutorial! Thanks.

    Reply
  22. Herny Louis says:
    Mar 6, 2012 at 4:09 am

    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.

    Reply
  23. zachshallbetter1 says:
    Mar 1, 2012 at 1:23 pm

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

    Reply
    • oneeezy says:
      Sep 10, 2012 at 1:08 am

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

      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
SeedProd Logo
SeedProd
Create beautiful custom landing pages - Drag & drop builder. 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]
    • 30 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 2020 (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 (2020)
    • 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 (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • 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 2020 – Step by Step Guide
Deals & Coupons (view all)
Weglot Coupon
Get 15% OFF on Weglot multilingual plugin for WordPress.
PanKogut
PanKogut Coupon
Get 20% OFF on PanKogut's premium WordPress themes collection.
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).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

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

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.