can’t limit search to only pages

A quick test on a default install with TwentyEleven confirms this, and I’m not sure why this is the case, however there is a way you can do this via your functions.php.

add a hidden field to the form with your own query var:

<input type="hidden" name="my_type" value="page" />

or:

<input type="hidden" name="my_type" value="post,page" />

add the new query var to WP’s recognized query vars:

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

add your code to set the post type when that query var is set:

function wpse50828_search( $query ){
    if( get_query_var('my_type') )
        $query->set('post_type', explode( ',', get_query_var('my_type') ));

    return $query;
}
add_action( 'parse_query','wpse50828_search' );

Leave a Comment