Do you want to create an advanced search form in WordPress for custom posts types?
If you use custom post types, then you may want to create a form that allows visitors to search only that content. This can help people find what they’re looking for and keep them on your site for longer.
In this article, we’ll show you how to create an advanced search form for custom post types in WordPress, step by step.
Why Create an Advanced Search Form for Custom Post Types in WordPress?
If you have lots of different content on your site, then a custom post type search form can often improve the visitor experience.
For example, we use custom post types here at WPBeginner to organize our deals and glossary sections. This keeps them separate from the rest of the content on our site.
On these pages we also use a custom post types search form, so visitors can search within the specific deals and glossary categories.
Often, this kind of advanced search form will help visitors find what they’re looking for faster, which encourages them to stay on your website. This will increase dwell time and reduce bounce rate, which sends positive WordPress SEO signals to the search engines.
It can also be useful if you’re running an online store. For example, you might create custom post types for different product categories, so shoppers can search within those categories. In this way, you can create a smarter product search and get more sales.
With that said, let’s show you how to create an advanced search form for custom post types in WordPress.
Create a New WordPress Search Algorithm with a WordPress Plugin
The easiest way to create an advanced search form for custom posts types is by using the SearchWP plugin. It’s the best search plugin for WordPress used by over 30,000 sites.
SearchWP is easy to use and and gives you full control over the search results.
First thing you need to do is install and activate the plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.
Upon activation, go to Settings » SearchWP and then click on the ‘License’ menu option.
Then, enter your license key in the ‘License’ box. You can find this information by logging into your SearchWP account.
After entering your license key, go ahead and click on the ‘Activate’ button. Once you’ve done that, it’s time to click on the ‘Engines’ menu option.
This brings you to a screen where you can configure the SearchWP search engine. Think of it like creating your own Google search algorithm for your WordPress website.
With SearchWP, you need to create a site-wide search engine before you can create a custom post types search engine, so we’ll do that first.
You can change the search engine settings by scrolling to the ‘Posts’, ‘Pages’, or ‘Media’ section. Each section has its own Application Attribute Relevance sliders that control how SearchWP ranks the content when building its search results page in WordPress.
For example, if you move the post’s ‘Title’ slider to the right and the ‘Content’ slider to the left, then a post’s title will carry more weight than the content.
Once you’ve finished customizing the default search engine settings, go ahead and save your changes by clicking on the ‘Save Engines’ button.
At this point you may see a message asking you to rebuild the index. If this happens, then simply click on the ‘Rebuild Index’ button.
Once you’ve created a default search engine, you’re ready to create an advanced search form for your site’s custom post types.
Add an Advanced Search Form Limited to a Single Custom Post Type
To create an advanced search form, we’ll need to create a separate search engine that only targets a particular custom post type.
First, go to Settings » SearchWP and then click the ‘Add New’ button.
This will automatically create a new search engine.
Next, simply click the ‘Sources & Settings’ button.
This will open a popup where you can select the sources that you want to use in your search engine, including any custom post types you’ve created.
To exclude a type of content from the search results, simply uncheck its box.
In this example, we’ll uncheck everything apart from the ‘Coupons’ box. This means that SearchWP will only search posts that have the ‘Coupons’ custom post type.
You also need to give your search engine a name by typing into the ‘Engine Name’ field. You’ll be using this name in a later step, so make a note of it.
When you’re happy with how the search engine is set up, click the ‘Done’ button.
Once you’ve done that, click the ‘Save Engines’ button to save your changes.
Now, we need to download the SearchWP Shortcodes extension so we can easily add the new custom post search form to our WordPress blog.
To do this, go to SearchWP » Extensions. Here, find the Shortcodes extension and click on its ‘Install’ button.
Once you’ve done that, you’re ready to create a new custom post search form page using code. To get started, simply open the page or post where you want to add the search form.
Then, click the plus ‘Add Block’ icon to bring up the blocks menu.
Next, go ahead and type ‘HTML’ into the search box.
When it appears, select the ‘Custom HTML’ block.
Once you’ve done that, it’s time to add the following code to the custom HTML block.
[searchwp_search_form engine="coupon" var="searchvar" button_text="Find Results"] <div class="search-results-wrapper"> [searchwp_search_results engine="coupon" var="searchvar" posts_per_page=4] <h2>[searchwp_search_result_link direct="true"]</h2> [searchwp_search_result_excerpt] [/searchwp_search_results] </div> <div class="no-search-results-found"> [searchwp_search_results_none] No results found, please search again. [/searchwp_search_results_none] </div> <div class="search-results-pagination"> [searchwp_search_results_pagination direction="prev" link_text="Previous" var="searchvar" engine="my_test_engine"] [searchwp_search_results_pagination direction="next" link_text="Next" var="searchvar" engine="my_test_engine"] </div>
This code will create a WordPress form that uses the search engine you created in the previous step. This means it’ll search your custom post types only.
In the code above, you simply need to change the engine name from
engine="coupon" to your own engine name.
The code will also create a section to display the results, show a ‘no results’ message if necessary, and add pagination if the results go into multiple pages.
When you’re ready to go live, simply click ‘Publish’ or ‘Update.’
Now if you visit your site you’ll see the custom post type search engine.
There are lots of different ways that you can improve WordPress search with SearchWP. Now you have the plugin setup, you may want to add live Ajax search to your website, create a search by category feature, or more.
We hope this article helped you learn how to create advanced search form in WordPress for custom post types. You may also want to see our guide on how to increase your blog traffic and our expert pick of the best live chat software for small businesses.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
How to me can add product post type in here?
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.
Sir.. can you plz attach screenshot with tutorials afterwords?? i think it’s more helpful for us..
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.
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/
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?
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 :).
Jonathan Stevens says
I had the same problem. Ended up with this messy solution, but it worked:
Dang.. still nothing. . .
Hi, there isn’t a way to tell the engine to automatically list all terms or tags or custom field? thanks
Editorial Staff 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:
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
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 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?
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 ?
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.
Merianos Nikos says
Very nice article
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.
Ever come across anything that explains this?
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?
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?
@CathieHeart That can be accomplished using the similar technique.
Does this work with a buddypress enabled site?
@mainaPlus Haven’t tried it with BuddyPress so not sure
Would this be able to work with a buddypress enabled site?
Editorial Staff says
Not sure about that. We don’t have much experience with BuddyPress.
Haven’t tried it with BuddyPress so not sure.
Abdelhadi Touil says
Very nice tutorial! Thanks.
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.
I would love to see a way to do this with custom fields as well.
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!!