I have a problem with search queries. Anytime you put additional queries, like:






it should do a search in the specific scope (post type, category, tag).

In my case, it doesn't seem to work no matter what I put as an additional parameter, it will display all results containing the word I'm searching for.

In my post types, I have query_var set to true so in theory, it should work.

My search.php loop looks like this

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    <div class="post">

        <?php if ( 'post' == get_post_type() ) : ?>
        <p class="category"><?php the_category(' '); ?></p>
        <?php else : ?>
        <p class="category">Resource</p>
        <?php endif; ?>

        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <?php the_excerpt(); ?> 

    </div><!-- /post -->

<?php endwhile; ?>

Using a hidden input in my searchform.php doesn't work either,

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>"  class="search">
    <input type="text" class="field" name="s" id="s" />
    <input type="submit" class="submit" name="submit" value="Go" />
    <input type="hidden" name="post_type" value="myposttype" />

it still searches normal posts, not only within myposttype CPT.

Any idea why the search queries wouldn't work? Any help appreciated.

2 Answers 2


I've found that passing post_type doesn't restrict searches to that type, but just adds that type to the array it already searches.

What you can do to build complex search queries is hook pre_get_posts and do a little query manipulation.

For a simple example, first I add my own query var to pass to WordPress's array of known query vars:

function wpa53029_query_vars( $query_vars ){
    $query_vars[] = 'my_type';
    return $query_vars;
add_filter( 'query_vars', 'wpa53029_query_vars' );

Then set up the form to pass a post type, like:



Then add some code to intercept that query var before the database is queried, and set post type:

function wpa53029_pre_get_posts( $query ){
    if( isset( $query->query_vars['my_type'] ) ){
        $types = explode( ',', $query->query_vars['my_type'] );
        $query->set( 'post_type', $types );

    return $query;
add_action( 'pre_get_posts', 'wpa53029_pre_get_posts' );

For debugging, print out the contents of the query object in your search.php template to see how everything is being set, and the actual SQL query WordPress generates to query the database. WordPress uses $wp_query as the global variable which holds the main query:

    <?php print_r( $wp_query ); ?>
  • should this work with more than one post type sent to the query? Search for keyword (mysite.com/?s=keyword) returns two posts from two different post types. When I search for that keyword and one of the post types (mysite.com/?s=keyword&my_type=typeone) it returns the correct post (and printing the $wp_query displays all information about it). However, if i search for both post types (mysite.com/?s=keyword&my_type=typeone,typetwo) it returns nothing, no posts and nothing in $wp_query. Any ideas?
    – Justine
    May 23, 2012 at 19:49
  • @Justine - it only accepts a single type, I've updated the wpa53029_pre_get_posts function above so you can now pass a comma-separated list of types and it will convert them to an array for the query.
    – Milo
    May 23, 2012 at 20:26
  • this helped passing a list of types to the query, however it started showing pages in my search results.. (I had a filter above that to only include 'post' and my custom post types: $query->set('post_type', array('post', 'typeone', 'typetwo', 'typethree')); Any reason why it suddenly started ignoring this filter? It's also set on pre_get_posts and worked with your previous snippet (with quering just one post type).
    – Justine
    May 23, 2012 at 21:47
  • any chance for last bit of help on this one? I'd be very grateful, can't figure this out :)
    – Justine
    May 24, 2012 at 6:39
  • It also started causing a problem with custom menus, as described here: wordpress.stackexchange.com/questions/53144/…
    – Justine
    May 24, 2012 at 11:16

I haven't tested this, so it might not work quite like I think it will, but try this:

$post_type = $_GET['post_type'];

global $query_string
query_posts( $query_string . '&post_type=' . $post_type );

// loop here
  • I'm not sure this will help me. I'm trying to build a more advanced search form with checkboxes for categories, post types and tags and need the URL queries to work properly. I just can't figure out why they don't.
    – Justine
    May 23, 2012 at 16:50
  • Didn't help; mysite.com/?s=lorem&post_type=myposttype still returns all post types as result. Unrelated, but it seems ?cat and ?tag suddenly started to work, so my only problem is post type now...
    – Justine
    May 23, 2012 at 17:06
  • Sorry I couldn't help.
    – Alex Lane
    May 24, 2012 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.