Include custom post types inside WordPress search

Looking to include custom post type results inside the WordPress search results? Here’s a guide on how to accomplish this.

Alter the search query

As default, WordPress does not include your custom post types inside the search results.

By using the code example below, you’re able to alter the search query to include these, before the posts have been fetched (using the pre_get_posts filter).

function wpza_include_cpt_in_search( $query ) {
     if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
          $query->set( 'post_type', array( 'post', 'cpt_name_one', 'cpt_name_two' ) );
     return $query;

add_filter( 'pre_get_posts', 'wpza_include_cpt_in_search' );