Skip to main content Skip to secondary navigation Accessibility Feedback

How to exclude pages from Search in WordPress

Up until a few weeks ago, if you searched for common terms like “mobile,” “jquery,” or “design” on this site, my about page would show up in the results. It was weird and not really relevant to what a person would typically be looking for when using search.

Fortunately, there’s a pretty easy way to configure your search results so that only posts (not pages) show up. Just add this snippet of code, courtesy of C. Bavota, to your functions.php file…

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');
  • More articles on...
  • Code

Have any questions or comments about this post? Email me at chris@gomakethings.com or contact me on Twitter at @ChrisFerdinandi.

Get the Weekly Digest