from the desk of aniketh

code and ideas

Search only within posts in WordPress

without comments

If you only want search to look within posts and not in pages for a wordpress site, then a quick and easy way to be to add the following code to your theme’s functions.php file :

add_filter('pre_get_posts', 'aa_search_query_vars');
 
function aa_search_query_vars($public_query_vars) {
    global $wp_query;
    if(is_search()) {
        $wp_query->query_vars['post_type'] = 'post';
    }
    return $wp_query;
}

Written by aniketh

February 24th, 2011 at 5:26 pm

Posted in Uncategorized

Leave a Reply