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]<?php get_template_part( ‘search’, ‘artwork’ ); ?>[/php]
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 thoughts on “Create custom search forms in WordPress

  1. 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.

    1. 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.

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

  2. 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?

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

  4. 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.

  5. 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.

  6. 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.

  7. 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

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

Leave a Reply

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

Ready for a refreshing experience on your next website design?