How to Add Custom Post Types to Search Results in WordPress

Including custom post types in search results allows you to make all relevant content on your site easily accessible to users. It can enhance your site's search functionality and user experience. By default, WordPress search results only include posts from the standard "post" post type. If you have custom post types on your site and want them to be searchable, you can easily achieve this using the "pre_get_posts" hook.

Suppose you have a custom post type called "recipes", and you want it to appear in search results alongside regular posts. You can do this by adding the following code to your themes's functions.php file:

function themesdna_add_custom_post_types_to_search( $query ) {
    // Check if it's the main search query and not in the admin area
    if ( $query->is_search() && $query->is_main_query() && !is_admin() ) {
        // Include the 'recipes' post type in search results
        $query->set( 'post_type', array( 'post', 'recipes' ) );
    }
}
add_action( 'pre_get_posts', 'themesdna_add_custom_post_types_to_search' );

Our WordPress Themes

Below are some of our premium WordPress themes. View all our free and premium WordPress themes →
GridFeel PRO WordPress Theme

GridFeel PRO

$25.00
GridNext PRO WordPress Theme

GridNext PRO

If you have used GridNext Free WordPress Theme, then you will love the premium version. GridNext PRO version has color...

$25.00
WP Masonry PRO WordPress Theme

WP Masonry PRO

If you like WP Masonry free WordPress Theme, you will love the premium version. WP Masonry PRO is an improved...

$25.00
GridBit PRO WordPress Theme

GridBit PRO

Are you looking for a feature-rich version of the GridBit Free WordPress Theme? Then you will fall in love with...

$25.00
GridHot PRO WordPress Theme

GridHot PRO

If you have tried GridHot Free WordPress Theme, then you will love the premium version. GridHot PRO version has color...

$25.00
BlogWP PRO WordPress Theme

BlogWP PRO

If you like BlogWP free WordPress Theme, you will love the premium version. BlogWP PRO is easy to use, feature-rich,...

$25.00
GridSoul PRO WordPress Theme

GridSoul PRO

$25.00
FineWP PRO WordPress Theme

FineWP PRO

If you like free version of FineWP WordPress Theme, you will love the premium version. FineWP PRO is an improved...

$25.00

Can't you choose a single theme? Purchase All Themes for $75.

Save money with our low, one-time price for access to all of our 56 WordPress themes.