How To Exclude Custom Post Types From WordPress Search Results

summertime flag bYTq59Lo91s unsplash scaled Running WordPress
This article can be read in about 1 minutes.

Paste the following code in functions.php to only allow posts and pages.

function exclude_custom_post_types_from_search($query) {
    if (!is_admin() && $query->is_search) {
        // Specify the post types to include in search results
        $query->set('post_type', array('post', 'page')); // Excludes all other post types
    }
    return $query;
}
add_filter('pre_get_posts', 'exclude_custom_post_types_from_search');

Comment

Donate with Cryptocurrency!

Copied title and URL