Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

mywebsite.com/?s=wordpress&post_type=page

mywebsite.com/?s=wordpress&post_type=page,post

mywebsite.net/?s=wordpress&cat=1

mywebsite.net/?s=wordpress&post_type=post&tag=genesis-post

mywebsite.net/?s=wordpress&post_type=post&tag=thesis-post

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

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

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

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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:

mywebsite.com/?s=wordpress&my_type=page

mywebsite.com/?s=wordpress&my_type=page,mycustomtype

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:

<pre>
    <?php print_r( $wp_query ); ?>
</pre>
share|improve this answer
    
Fantastic! Works perfectly, thank you so much. –  Justine May 23 '12 at 18:34
    
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 '12 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 '12 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 '12 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 '12 at 6:39
show 3 more comments

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
share|improve this answer
    
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 '12 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 '12 at 17:06
    
Sorry I couldn't help. –  Alex Lane May 24 '12 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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