How to Create Advanced Search Form In WordPress Website

Advanced Search Form In WordPress

How To Create Advanced Search Form In WordPress For Custom Post Types Using Code?

WordPress built-in search option is quite ineffective and hence, the only option people opted for is Google Custom Search.

But there are many owners who do not like to use Google Custom Search especially when they have custom post types like reviews, videos, image-related photos along with general informative posts.

It is a fact that Google Custom Search option is not an impressive option and many feel like it reduces the user experience significantly.

It is always handy to create advanced search form in WordPress for custom post types where the visitors of the website can select the types of posts where they want to do the search.

For examples, all the post types like videos, reviews, and others will be provided in checkbox style, and users will be able to select the post types where they want to search for something. As a matter of fact, they can select multiple types of posts to search from and get all the relevant search results. In this article, we will illustrate how to create advanced search form in WordPress for custom post types.

Most of the themes on WordPress come with two very important files as far as searching goes. One of them is searchform.php. The location of the form might vary from theme to theme, and hence, you have to connect to your FTP and search for it if it is not available under Editor option from your Admin Dashboard’s Appearance option. Sometimes, the file name may also vary and hence, you may contact your theme’s support team to know its name.

Once you find the file, edit it and add the following piece of code into it.




$query_types = get_query_var('post_type');   
<label>Articles</label>
<label>Blog</label>
<label>Books</label>
<label>Videos</label>  



Then you need to edit the value with the name of your custom post types as they exist of your website. If you have more custom post types, you can add similar lines and replace the values accordingly. It is important to note that you should not add Pages custom post type here. This code is added to create an array so that it could be called in the next file and all the checkboxes can appear checked when the user sees the box for the first time. So, by, default, the searching will be done on all the post types.

Now you need to open the second most important search file named search.php. You are likely to get to from going to Admin’ Dashboard, Appearance and then to Editor. Locate the file from the right side under the label Search Results. Add the following piece of code in that and update the file.




form role=&quot;search&quot; method=&quot;get&quot; id=&quot;searchform&quot; action=&quot;"&gt;
input type=&quot;text&quot; name=&quot;s&quot; id=&quot;s&quot; value="" value="Enter keywords &hellip;" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"
 
<label>Articles</label>
input type=checkbox name=post_type[] value=post <label>Blog</label>
input type=checkbox name=post_type[] value=books <label>Books</label>
input type=checkbox name=post_type[] value=videos; <label>Videos</label>  


Once again, the value of the input boxes must be same as the custom post types you have on your website and should also be similar to what you have in your searchform.php file. This will show the checkboxes to the users in alredy marked state and search accordingly. Many would prefer a dropdown but it is always better to go with checkboxes because users will be able to select more than one option.

How To Create Advanced Search Form In WordPress For Custom Post Types Using Plugins?

It is safe to say that some people would not be able to do the above task of coding correctly because they have to search for searchform.php and hence, there are high chances of messing things up. It is wise to go for a tailor-made search form plugin which is designed for custom post types. Search & Filter Pro is the plugin you need to go for, and it offers you to have fields like custom post types, taxonomies, categories, authors, post dates and much more. For any review site, it is a must. If you have an e-commerce site using WooCommerce, you can provide your visitors great user experience with it.

The price of the plugin is merely $20 and if you are a developer, you can go to developer license for $75 and create amazing parameters. You have to pay the price and get the plugin downloaded and then install on your WordPress site. To install it, you have to go to Dashboard and then to Plugins on the left menu where you have to click on Add New option from the dropdown. In the next screen, you will find the button Add New at the top. Select the zip file you have downloaded and let it get uploaded and then Activate it. You will get one year of support and upgrades for free.

The plugin works with multilingual support(WPML), WooCommerce, and Advanced Custom Fields plugins where you can attach dropdown, checkboxes, and various other fields. Various other frontend UI elements can be used like date picker and range sliders. There are also options for sorting the result by various parameters like the ones you get with advanced Google search.

If you are going to create advanced search form in WordPress for custom post types using codes, make sure you take a full backup of your website. If you find that your search toolbar is getting used widely by the visitors of your site, you should definitely go for the Pro plugin and offer different parameters that you cannot create with the code that we provided above. The more functionalities you provide, the better would be the user experience, and your website will be the go-to website in your niche for that feature only.

About SKT Posts Themes

SKT Posts Team shares exciting WordPress themes, plugins and other WordPress related news for our viewers. We also post selected WordPress developers interviews from time to time.

SKT Free Software Theme