Searching multiple custom post types and pages

The question:

I’m using bainternet’s method for searching custom post_types and it works great. However, I’ve recently been requested to return more than one “specific” post type and perhaps individual pages for a support section on our site.

I thought adding additional hidden fields would do it, but that didn’t work. And I have no clue how this will work with pages.

Any help would be appreciated!

In the template:

<form action="" id="searchform" method="get" role="search">
    <div><label for="s" class="screen-reader-text">Search for:</label>
    <input type="text" id="s" name="s" value="">
    <input type="submit" value="Search" id="searchsubmit">

    <input type="hidden" name="post_type" value="software" />

In functions.php:

/* Custom Search Queries */
function SearchFilter($query) {
$post_type = $_GET['post_type'];
if (!$post_type) {
    $post_type = 'any';
if ($query->is_search) {
    $query->set('post_type', $post_type);
return $query;

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1


<input type="hidden" name="post_type" value="software" />


<input type="hidden" name="post_type[]" value="software" />
<input type="hidden" name="post_type[]" value="books" />

i have to run but this should work , just add as many hidden fields as you need for each post type

Method 2

You could do it like this to switch between a singular set type and any..

<input type="hidden" name="post_type" value="software" />
<label for="post_type">Search all</label>
<input type="checkbox" name="post_type" value="any" />

Unchecked searches your specific type, checked sets the post type to any..

Method 3

To me works that solution (same as Bainternet’s, but without “[]” after “post_type”):

  <input type="hidden" name="post_type" value="photo">
  <input type="hidden" name="post_type" value="video">

And to get a specific template for search results based on the post type(s) (on functions.php):

function search_template_chooser($template) {
  global $wp_query;
  $post_type = get_query_var('post_type');
  if( $wp_query->is_search && $post_type == 'photo' || $wp_query->is_search && $post_type == 'video' ) {
    return locate_template('search-media.php');
  return $template;

add_filter('template_include', 'search_template_chooser');

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment