updated   : May 15, 2012
we now have 588 snippets
By : , on May 06, 2011 9:00 am

Create multiple search templates for custom post types

wordpress snippet

Adding these snippets to your wordpress theme will enable you to setup multiple search templates for custom post types.

Search template

Create a new file called search.php and add the following search template. Change the $search_refer= CUSTOM_POST_TYPE to the names of your post types. You will also need to change the template path to the corresponding template you wish to display results.

snippet :  PHP - SEARCH TEMPLATEcopy
<?
/* Template Name: Search Results */
$search_refer = $_GET["post_type"];
if ($search_refer == 'CUSTOM_POST_TYPE') { load_template(TEMPLATEPATH . '/template_one-name.php'); }
elseif ($search_refer == 'CUSTOM_POST_TYPE') { load_template(TEMPLATEPATH . '/template_two-name.php'); };
?>

Display search results

Add this query_post just above the loop in the search templates that you create. Don't forget to change the CUSTOM_POST_TYPE for each of your templates.

snippet :  PHPcopy
<?php
	$args = array(
		'post_type'=> 'CUSTOM_POST_TYPE',
                's'    => $s);
		query_posts($args);
?>

Search form

Add this HTML to the template you wish to display the search form. You will need to change the CUSTOM_POST_TYPE name to the post type you wish to search. You will need to create a new form for each custom post type or use a select menu to set the post_type.

snippet :  HTMLcopy
<form id="searchform" action="<?php bloginfo('home'); ?>/" method="get">
	<input id="s" maxlength="150" name="s" size="20" type="text" value="" class="txt" />
	<input name="post_type" type="hidden" value="CUSTOM_POST_TYPE" />
	<input id="searchsubmit" class="btn" type="submit" value="Search" />
</form>
  • Lin D.

    This didn’t work on mine

    • http://wpsnipp.com Kevin Chard

      Hi Lin D. this does work I have it running with a number of clients. If you wanted to email me with this form bellow I can help you further via email to get things running and even take a look at your templates if you would like.

      http://wpsnipp.com/contact/

  • Anonymous

    This does work (brilliantly! thank you Kevin) only problem is there is a misplaced } in the
    Display search results section. This might be the source of Lin D’s problem.

    • http://wpsnipp.com Kevin Chard

      @toomanyairmiles:disqus  glad to hear that you like the snippet and thanks for the heads up for that extra } I updated the snippet.

  • http://www.facebook.com/flavioleonardvargas Flávio Leonard Vargas

    Working Great! Thanks a bunch!

  • http://www.cliffpaulick.com Cliff Paulick

    Ooh, great! What about form fields based on taxonomies or custom fields, like for a real estate search field? Just reading the snippet, it looks like a regular free-form search box that searches the post type of the post you’re currently viewing. What if it’s a search box on the homepage or another non-CPT? It doesn’t look like there’s a fallback to a regular ‘ol WordPress search. Please correct if wrong.

  • http://www.facebook.com/vadim.roundhouse Vadim Goncharov

    How do you set up something where “nothing found” messages appears if there is no records in the db? 

  • Tom

    I have a problem with pagination, when custom post type search result is more than 5.

    Any advices?