How to Exclude Pages from Your WordPress Search Results

I was recently working on a project that required excluding pages from the built-in WordPress search functionality. After much googling, I came across the small bit of code I was looking for here in the WordPress forum.

Just add the following code to your functions.php file to exclude pages from your WordPress search:

<?php
function mySearchFilter($query) {
    if ( ! is_admin() && $query->is_search ) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','mySearchFilter');
?>

One thought on “How to Exclude Pages from Your WordPress Search Results

Leave a Reply

Your email address will not be published. Required fields are marked *