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">Search Artwork</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
<?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
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.
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.