Are you wondering how to create a WordPress search form for custom post types?
Adding a separate search form for different content types can be handy if you have a lot of pages on your site.
It makes finding a specific type of content much easier and guarantees a better user experience for your visitors.
In this step-by-step tutorial we’ll show you how to create a dedicated search form for custom post types.
Let’s dive in.
Why Create a Search Form for WordPress Custom Post Types?
Let’s start by defining what custom post types are and why you may want to add a search form to them.
Usually, WordPress supports only 2 types of content: posts and pages.
However, you’re free to create your own custom posts types and add any custom fields to it.
This can be very useful if you’re creating content that doesn’t fit within the boundaries of a standard post or page.
For instance, if you run a cooking blog, you can create a custom post type and name it “Recipe”.
Then, for the custom fields, you can use “ingredients”, “cooking methods”, and “estimated cooking time”.
Here’s another example.
A custom post type at WPBeginner on their Deals section, where they collect featured deals for their visitors:
Simply put, custom post types allow you to turn WordPress from a simple blogging platform into a full-fledged content management system.
Now, why create a search form that searches exclusively among certain custom post types?
Because you can make it much easier for your visitors to find the content they’re looking for.
For example, what if you add a search form that searches only for the “Recipe” custom post type?
It ensures that by typing a “chicken” search query, visitors will find a chicken recipe only – not an article, news, or just a page about chicken.
Another thing to consider is that the native WordPress search engine completely ignores custom fields.
That means that even though your visitors can find a chicken recipe, they still can’t search for specific meal ingredients.
Don’t worry, because below, we’ll show you how to create a search form for custom post types and make WordPress consider custom fields when searching.
How to Create a WordPress Search Form for Custom Post Types
The easiest way to create a dedicated WordPress search form for custom post types is to use a plugin like SearchWP.
SearchWP is the best WordPress search plugin used by more than 30,000 websites.
Using this plugin, you can customize the WordPress search engine for your needs.
SearchWP lets you have complete control over your WordPress search results. You can use it for:
- Taxonomies and shortcodes indexing. By default, WordPress ignores taxonomies and shortcodes while searching. Using SearchWP, you can make the data within them searchable and provide your visitors with expanded search results.
- On-site search activity tracking. Do you wonder what kind of content your visitors are looking for? SearchWP provides you with a comprehensive report on the search behavior of your site’s visitors.
- Seamless WooCommerce integration. SearchWP allows you to make attributes, tags, and reviews of your WooCommerce products searchable. The best thing is that you can do it in just a few clicks.
Now, let’s talk about how you can use SearchWP to create a WordPress search form for custom post types.
Step 1: Install and Activate SearchWP
To begin with, you want to get your copy of SearchWP here.
Once you’re done, open the Downloads tab in your SearchWP account.
Then, press the Download SearchWP button and save the plugin to your computer.
Once done, copy the plugin license key in the email you received after getting your copy of SearchWP.
Next, go to your WordPress dashboard and upload the SearchWP plugin ZIP file you’ve just downloaded.
If you’re not sure how to do that, please check out the how to install a WordPress plugin step-by-step tutorial.
The next step after installing and activating SearchWP is to enter the license key.
To do that, click the SearchWP button that appears at the top of your WordPress dashboard.
Then, navigate to the License tab.
Enter your SearchWP license key on the appropriate field and click Activate.
Once the license is activated, you can proceed to set your WordPress search engine settings.
Step 2: Add a New Search Engine to Your Custom Post Type
The next step is to add a new search engine to your site. Think of a search engine as it’s a set of rules by editing which you can determine how WordPress performs a search.
To get started, go over to the Engines tab in SearchWP settings.
This brings you to the Engines dashboard, where you can set your search engine settings.
For example, you can customize the priority of specific post or page attributes, add and remove search sources, exclude results with particular name patterns, and many more.
Think of it as if you were adjusting Google’s search algorithm, but only for your own site.
SearchWP lets you use multiple search engines simultaneously.
So, before creating a search form for custom post types, let’s add a new search engine specific to it.
To do so, click Add New at the Engines tab.
This will automatically create a new separate search engine for your specific custom post type.
Then, click Sources & Settings at the top right of the newly created search engine.
Here, you can select the sources you want this search engine to consider when performing a search.
In this tutorial, we will create a search form that searches exclusively among the custom post type “Products.”
To do so, we’ll uncheck all the boxes except Products.
Also, we’ll change the standard search engine label from “Supplemental” to “Products”. It’ll help to recognize the engine more easily in further.
When you’re finished, click Done.
Then click Save Engines at the SearchWP settings tab to save your new custom post type search engine.
The next step is installing and activating the SearchWP “Shortcodes” extension.
Step 3: Install and Activate the Shortcodes Extension
This extension allows you to add a search form to your custom post type in just a few clicks.
To grab it, go to the Shortcodes extension download page and press the Download Extension button.
Then, install and activate the extension the same way you installed SearchWP.
Once you’ve installed and activated the Shortcodes extension, you can proceed to create a new custom post search form page.
Step 4: Create a New Custom Post Search Form Page
First, navigate to Pages » Add New from your WordPress dashboard.
Name the page the way you want and press the Plus button to add a new block.
Then, find the Custom HTML block and click on it to add it to the page.
Next, copy and paste the following code into the newly created HTML block:
[searchwp_search_form engine="products" var="searchvar" button_text="Find Results"] <div class="search-results-wrapper"> [searchwp_search_results engine="products" 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="products"] [searchwp_search_results_pagination direction="next" link_text="Next" var="searchvar" engine="products"] </div>
Make sure that you replace the name of the engine “products” with the name of your own engine that you created in the previous step.
The above code brings up a full-fledged search box on the page.
Using it, your visitors can only perform searches among the custom post types you’ve indicated in the settings of your search engine.
Once done, click Publish and then View Page to go to your page.
Your custom post type search engine is now available on your WordPress site.
Let’s test our search box. We’ll enter the search term “cookies”, press Find Results and see what sort of posts it displays.
As you can see, the search results contain only the “products” custom posts type, meaning that our new search form works just fine.
So, in this article, you learned how to create a WordPress search form for custom post types.
If you’re ready to create your own search form for custom post types, you can grab your copy of SearchWP here.
Also, it might be helpful for you to learn how to add a custom WooCommerce search widget if you’re the owner of an eCommerce WordPress site.
Want to increase the number of search results? Learn how to add fuzzy search to your WordPress site in a few simple steps.