AltDan
AltDan

Reputation: 844

Wordpress custom search page for custom post type

I have a custom post type recipes which displays a paged list of recipes on the site using the archive-recipes.php template.

I need to be able to search through these recipes using the search form at the top of the page:

<form role="search" method="get" class="searchbox" action="/recipes/">
    <input type="text" class="textbox strong" name="s" id="s" value="" placeholder="Search..." />
    <input type="hidden" name="post_type" value="recipes" />                     
    <button type="submit" class="button icon"></button>
</form>

Is it possible to return to this recipe listing page once the search has been performed and display the results in the same style as the listings?

I can't seem to find anything that will enable me to create a custom search page for custom post types.

Thanks for your help.

Upvotes: 1

Views: 8307

Answers (1)

Tom
Tom

Reputation: 1366

You could use the 'template_include' filter.

e.g. in your functions file.

function template_chooser($template)
{
  global $wp_query;
  $post_type = get_query_var('post_type');
  if( $wp_query->is_search && $post_type == 'recipes' )
  {
    return locate_template('archive-recipes.php');
  }
  return $template;
}
add_filter('template_include', 'template_chooser');

This should check for a search on the 'recipes' custom post type and use your archive page template for the results.

Upvotes: 3

Related Questions