Create Multiple Search Templates for Custom Post Types

Snippets by IsItWP

Are you looking for a way to create multiple search templates for custom post types? While there’s probably a plugin for this, we have created a quick code snippet that you can use to create multiple search templates for custom post types in WordPress.


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.

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

	$args = array(
		'post_type'=> 'CUSTOM_POST_TYPE',
                's'    => $s,
                'paged' => $paged,

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.

<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" />

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 43 best photography themes for WordPress and how to create a contact form in WordPress.

Comments  Leave a Reply

  1. Sally Pilkington January 7, 2019 at 3:09 pm

    Hi, Thanks for this tutorial.
    I’m trying to create a custom search form to place in the sidebar of my website’s blog section. I want the search form to only look within ONLY posts (standard blog posts, not a custom post type) – and to ignore any other content, eg.pages. Is it possible to adapt this method to suit what I’m trying to do?
    I’ve had a go but with no luck.
    Many thanks

    1. Hey Sally,
      Try this tutorial on how to filter post types from search results. Hope you’ll find it helpful 🙂

  2. Purr purr purrrrfect )

  3. Pitico Medtler July 3, 2015 at 2:16 pm

    Perfect. Thanks!

  4. Sorry for the noob question, but is there a way to take a level further and to only search a custom meta_field within the custom post type? I’ve been searching for days and this is the closest script I could find. Right now it searches the titles and content of the post, but I’ve got a ton of custom fields for the CPT and only want to the user to search specific field. Thoughts?

  5. is it possible to adapt these instructions to search a custom taxonomy instead of a custom post? Trying to return a separate search for each custom taxonomy and I’m not quite figuring it out. Thanks!

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!