Home / Blog / WordPress
,

How to include Custom Post Type in WordPress Search Result?

First published 25 Feb 2018. Tested again in November 2026 on WordPress 7.1.4 and PHP 8.4.

In the WordPress default search, custom post types are not included in search results. So in order to include custom post types in the search result, we have two methods: one using custom code in your theme functions.php file and second using third party free WordPress plugin.

Custom Method

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

function include_cpt_search( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        $query->set( 'post_type', array( 'post', 'page', 'custom_post_type' ) );
    }
}
add_action( 'pre_get_posts', 'include_cpt_search' );

The check limits the change to the search results page on the front end, so searches in wp-admin and other queries are left alone. Replace custom_post_type with your own post type’s slug.

View or fork this snippet on GitHub Gist.


Using WP Extended Search WordPress Plugin

To include Custom Post Types in WordPress Search, you can also use third party free WordPress Plugin WP Extended Search. WP Extended Search provides the feature to include/exclude Tags, Categories, Custom Post Types, Taxonomies, and Custom Meta Keys in default WordPress Search.

You can read in brief about the usage and features of the WP Extended Search plugin in my post: How to Improve WordPress Search with WP Extended Search.

Need this done on a live site? Exivon Tech takes on projects and audits, led by me.

Dhiren Patel

Principal Full-Stack Engineer · Founder, Exivon Tech

I’ve built and run enterprise web platforms and e-commerce for 13 years, and I built ShieldScope, a free WordPress security scanner.

2 responses to “How to include Custom Post Type in WordPress Search Result?”

  1. Adam

    Running this in the WP admin posts search I get
    Notice: Array to string conversion in D:\wwwroot\enhabit\wp-admin\edit.php on line 327

    1. Dhiren Patel

      Hi Adam,

      The notice message you showed doesn’t seem related to the code in the post. Can you be more specific about the issue that you are having, so that I can help you with the issue?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.