Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Add the below 2 snippets to a theme to include a Product only search, search results are displayed on search.php template. Update as required.
// Custom Product search
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'product' )
{
return locate_template('search.php'); // redirect to search.php
}
return $template;
}
add_filter('template_include', 'template_chooser');
<h3>Search Products</h3>
<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="text" name="s" placeholder="Search our products"/>
<input type="hidden" name="post_type" value="product" /> <!-- // hidden 'product' value -->
<input type="submit" alt="Search" value="Search" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment