Include custom post types in WordPress default search result

WordPress default search only works for post and page. When you search with the default search form, it will only look for a result within posts and pages. If you have custom post types, that will be ignored. Also you may want to exclude post or page from search results. This can be achieved by setting/modifying post_type property of the default search query. Default search query is just another WP_Query. So we can easily hook it with pre_get_posts action.

function careless_search_posts_type_only( $query ) {

    if( is_admin() ) {

    $post_types = array( 'post', 'portfolio' );//add/edit/remove your post types as necessary 

    if( is_search() && $query->is_main_query() ) {
        $query->set( 'post_type', $post_types );

add_action( 'pre_get_posts', 'careless_search_posts_type_only' );

Modify line 7 to add/edit/remove your required post type/s.

Code goes in the functions.php file of your active theme.