Create custom search forms in WordPress

Are you looking to add a separate, custom search form in WordPress and limit it to say, a specific Custom Post Type? Here’s how I did it:

1. Create a search form template

While you could paste the new form directly into a template, I chose to create a separate template to house the form: artwork-search.php (My custom post type is Artwork). This search function was designed to limit the results to only the Artworks Custom Post Type. Here is the HTML5 style form I used in this template:

<div class="widget artwork-seachform search" rol="search">

    <h3 class="widget-title"&gt;Search Artwork&lt;/h3>

	<form role="search" action="<php echo site_url('/'); ?>" method="get">

		<input type="search" name="s" placeholder="Search: Enter artwork keywords hit enter"/>

		<input type="hidden" name="post_type" value="artworks" /> <!-- // hidden 'your_custom_post_type' value -->

		<input type="submit" alt="Search" value="Search" />

	</form>

</div>

The key is the ‘hidden’ input field. Change the value to your own custom post type name.

2. Load the template part into your template

Now you can load that file into your template using get_template_part :

<?php get_template_part( 'search', 'artwork' ); ?>

The form will now appear an you can style it as you would normally. But what about the search results? What template gets used?

3. Post Type Archive template

To display the search results, WordPress will first look for archive-$posttype.php if you have it. Otherwise it will fall back to index.php. If you happen to use Hybrid Core you can also fall back to archive.php.

The great thing in my case is that I’d already styled the archive-artworks.php template, so my search results looked fantastic, appearing just as the normal artwork archive pages do.

Conclusions

I’ve seen some pretty complex tutorials for creating a separate search form in WordPress. This method worked for me and was overall pretty simple.

This method may or may not work using other WordPress content types. I tried adding a custom taxonomy to the hidden input, but it wouldn’t return any results.

Update: Sebastian points out in the comments that you may need to name your search file search-artwork.php. I use Hybrid Core which uses a modified template hierarchy which is why I named it the way I did.

21 Comments


Nice article, there is a mistake on the first line, it should be search-artwork.php for the template part otherwise it wont load when called.

Reply

    Thanks Sebastien, I\’ve updated the post to reflect this. Since I build on Hybrid Core, the modified template hierarchy requires the naming convention that I used. It would probably be better to write the tip from the point of view of non-Hybrid Core users, but I\’ve used it for so long that I sometimes can\’t separate it from standard WordPress development.

    Reply

      Thanks for the reply, i didn\’t know about hybrid core but i will look into it. Might come in useful


Hi,
Thanks for the tip. How can I create a page that will show the search bar and query a custom post type I created?

Reply

Your article is excellent but it search only one field. I want to search 2 field of a post type how can i do?

Reply

    Can you elaborate Chinmay? Not sure what you mean by searching \’2 field of a post type.\’

    Reply

Hello. sir Thanks for your valuable tips. can you please visit my site http://apnojaipur.com and can help me to create and embed the exact search form and search code to meet my needs. Thanking you in advance.

Reply

I also had chest and coronary heart problems, and I used
to be highly careworn. I consulted many docs from Taiwan and
USA.

Reply

Right this moment social networking web sites are very well-known and most of them spend time on one of these social network sites daily.
Do you want to see how I get fantastic looking website up and running that start generating revenue for me almost instantly.

So, be confident and remember that you can make money online with
your knowledge or skills that are better than others.

Reply

Make a complete list of gold-making quests, how to complete and their corresponding rewards.
Do you want to see how I get fantastic looking website
up and running that start generating revenue for me almost instantly.

So, be confident and remember that you can make money online with your knowledge
or skills that are better than others.

Reply

Yes! Finally someone writes about best bitcoin ptc sites.

Reply

No matter if some one searches for his essential thing, thus he/she desires to be available that in detail,
thus that thing iis maintained over here.

Reply

Great stuff Bryan! Sadly, im having a bad case of “The n00bs” right now, haha.

I’d love a custom search form with this post type: wprss_feed_item. So, using the code from your tutorial, would i simply do the following?

Create a new template file and throw it under my theme directory? (correct me if im wrong about this location)
Paste your code almost word-for-word, replacing only value=artworks with wprss_feed_item?

And finally the most important part is to display said form in a widget (either as a proprietary widget or through a shortcode)

I feel like i ALMOST have it working, but….it just wont work.
Enjoy your week!

Elvis

Reply

I need a help in customizing my search box
as I am using plugin “Search Everything” but it does not give the desired output

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Check out our Complete Website Package

Chat with us about your project or click below to find out more!

Learn More   Chat with us

Hi. We are Spigot. Telemarketers pronounce it Spy-got.

We are a Park City, Utah based web design shop that excels at building custom websites powered by WordPress and WooCommerce. We love content - content strategy, content curation, content soup...????Have a look around the site. As you browse you'll find useful navigation links in the sidebar on the left. We hope you find them useful anyway... Please let us know what you think, shoot us a message here.