Víctor GeisExclude Pages from WordPress Search

Exclude Pages from WordPress Search

Copy and paste this function into your functions.php file of your WordPress theme:

function vg_exclude_pages_from_search_results( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
        $query->set( 'post_type', array( 'post' ) );
    }    
}
add_action( 'pre_get_posts', 'vg_exclude_pages_from_search_results' );