How To Exclude Custom Post Types From WordPress Search Results

summertime flag bYTq59Lo91s unsplash scaled
Genx Avatar
Share This:

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');

Related Posts

Likes, Bookmarks, and Reposts

  • Genx Notes

Genx

in

Comment

Likes

Leave a Reply

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

5 × one =

Webmention: Have you posted a response to this article? Let me know the URL: