Search queries don’t seem to work?

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>