How to Create a WordPress Search Form for Custom Post Types
Do you want to create an advanced search form in WordPress for custom post 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 are looking for and keep them on your site for longer.
In this article, we will 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?
Custom post types allow you to go beyond posts and pages and make different content types for your website. For example, we use custom post types here at WPBeginner to organize our Deals and Glossary sections.
However, WordPress doesn’t include custom post types in its search results by default. As a result, visitors may miss out on important content, and you will miss out on pageviews.
Thankfully, you can include custom post types in your search results using a WordPress search plugin. You can even go one step further and create an advanced form that searches custom post types only.
For instance, at WPBeginner, we have created custom post type search forms on our Deals and Glossary pages.
Often, this kind of advanced search form will help visitors find what they are looking for faster, which will improve the visitor experience and reduce the bounce rate.
First, you can see our guide on how to make custom post types in WordPress. Then, you can jump into our tutorial on how to create an advanced WordPress search form for custom post types.
Create a New WordPress Search Algorithm With a WordPress Plugin
The easiest way to create an advanced search form for custom post types is by using the SearchWP plugin. It’s the best search plugin and allows you to search content that WordPress ignores by default.
For example, you can make blog comments searchable and add PDF indexing and searching to your website.
How to Set up SearchWP
The first thing you need to do is install and activate the SearchWP plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.
Upon activation, you must go to SearchWP » Settings and click on the ‘General’ tab.
You now need to enter your license in the ‘License Key’ box. You can find this information in your account on the SearchWP site.
Once you are done, make sure to click the ‘Verify Key’ button.
Creating a Custom Search Algorithm
Your next task is creating a custom search algorithm. It allows you to specify the content that SearchWP includes in its search results, like pages, posts, media, and any custom post types.
To get started, you need to go to SearchWP » Algorithm in your WordPress admin dashboard.
Here, you will see a section for every content type that SearchWP includes in its search results.
To add your custom post type, just click the ‘Sources & Settings’ button.
In the popup that appears, check the box next to each custom post type that you want to include in the search results.
After that, click on ‘Done’.
SearchWP will add a new section for each custom post type. Each section shows all the attributes that SearchWP will include in its searches, such as the title, slug, and excerpt.
If you want to add or remove attributes, then simply click on ‘Add/Remove Attributes’.
In the popup, check the box next to each attribute you want to include in the search results.
To remove an attribute, you can uncheck its box instead.
In this popup, you can also make custom fields searchable in WordPress.
When you are happy with the changes you have made, go ahead and click on ‘Done’.
You will notice that each attribute also has an Application Attribute Relevance slider. This controls how SearchWP ranks the content when building its search results page in WordPress.
If an attribute has high relevancy, then it will have a bigger impact on the search results. By contrast, content that matches an attribute that has less relevancy will appear lower in the search results.
For example, if you move the ‘Title’ slider to the right and the ‘Content’ slider to the left, then a post’s title will carry more weight than the content.
Every WordPress website is unique, so you may want to try different relevancy settings to see what gives you the most accurate and helpful search results.
For example, if you have created a ‘customer reviews’ custom post type for your online store, then this is important content, so you may want to give it a high relevance.
When you are happy with how the search feature is set up, just scroll to the top of the screen and click the ‘Save’ button.
SearchWP will now rebuild the index automatically. Depending on the speed of your Internet connection and your WordPress hosting provider, this may take a few minutes.
As soon as you see ‘Index Status 100%’, you will know that SearchWP has added your custom post types to its search index.
Your WordPress blog, website, or online marketplace will now include custom post types in its search results.
Create an Advanced Search Form for Custom Post Types
With that done, you are ready to make an advanced form that searches your custom post types only.
To get started, head over to SearchWP » Search Forms and click the ‘Add New’ button.
This creates a new form, ready for you to customize.
To give this form a unique name, click on the small pencil icon next to ‘Search Form 1’.
You can now type in a name for the custom search form.
This is just for your reference, so you can use anything that helps you identify the form in your WordPress dashboard. This is particularly important if you plan to create multiple search forms in WordPress.
After that, you can choose the layout you want to use using the ‘Layout Theme’ thumbnails.
Simply click on the different themes to see a preview of how this style will look on your WordPress website.
To create a search form for custom post types, you will typically want to use either ‘Basic’ or ‘Quick Search’.
These styles don’t allow visitors to search by category, which helps to keep them focused on custom post types.
After making your decision, scroll to the ‘Custom Styling’ section.
The settings you see may vary depending on your theme layout. For example, if you selected ‘Quick Search’, then the ‘Quick Search’ setting will be enabled by default.
You can disable and enable the different options using their toggles.
The live preview will update automatically as you make changes, so you can try different settings to see what works the best.
By default, the form will search media, pages, and posts and ignore custom post types. To include custom post types, you need to find the ‘Type of search’ field and give it a click.
You can now type in the name of each custom post type.
When the right post type appears, give it a click to add it to the search.
Do you want to search custom post types only and ignore all other content?
Then just click the small ‘X’ next to ‘Media’, ‘Pages’, and ‘Posts’.
With that done, the form will search your custom post types only.
When you are happy with how the form is set up, scroll to the ‘Form Style’ section. Here, you can choose between different form styles, add a border, change the font size, and more.
Under ‘Button Style’, you can customize the ‘Search’ button that appears in the form.
When you are happy with how the form is set up, scroll to the top of the screen and click on ‘Save’ to store your settings.
How to Add the Custom Post Types to WordPress
Now, you are ready to add the search form to WordPress using either a shortcode or block.
The easiest method is to use a block, so head over to the page or post where you want to add the custom post type form.
You can then click on the ‘+’ icon and type in ‘Search Form’.
When the right block appears, give it a click.
Next, open the ‘Select a Form’ dropdown menu and choose the search form you made during this tutorial.
When you are ready to make the search form live, simply click on either ‘Update’ or ‘Publish’. Now, you can visit your website to see the search form in action.
Another option is to add the search form using a shortcode.
To get the shortcode, you need to head over to SearchWP » Search Forms. Here, find the search form that you want to show and copy the value in the ‘Shortcode’ column.
You can now add the shortcode to any page, post, or widget-ready area. For step-by-step instructions on how to do this, please see our guide on how to add a shortcode in WordPress.
We hope this article helped you learn how to create an advanced search form in WordPress for custom post types. You may also want to see our custom post types tutorials and our expert picks for the best WordPress plugins to grow your website.
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.
این خبر را در ایران وب سازان مرجع وب و فناوری دنبال کنید