How to Include Custom Post Types in WordPress Search Results

By default, custom post types are not included in the search results, so we are going to create a function that allows them to be queried and searched by WordPress.

You can include custom post types in the search result by adding below function in your functions.php file.

    function includePostTypeInSearch( $query ) 
    {

        if ( $query->is_search ) {
            $query->set( 'post_type', 
              array( 'post', 'listing',   'news', 'books' ) );
        }
        
        return $query;
    }

    add_filter( 'pre_get_posts', 'includePostTypeInSearch'); 

Leave a Comment

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