How to include search results from pages in WordPress?

How to include search results from pages in Wordpress?

WordPress is an open-source CMS known for blogging and news websites, which relies on posts more than pages. The default search function returns results from posts only, but not from pages.

The WordPress search function will be useless for evergreen content websites as they are built using pages, not posts.

So how to make the WordPress search function return results from pages instead of posts?

Fortunately, there’s a solution for this without hiring a developer.

Simple, go to WordPress Dashboard -> Select Theme File Editor under Appearance -> from the right-hand sidebar, select Functions.php file – add the following code to the very end of the file -> Save.

Now, test your WP Search function. You will notice that search results do now return with results from pages.

 

function wpshock_search_filter( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array('post','page') );
    }
    return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');

Please drop us a line if you need any help.