Exclude any custom post type from WordPress search

This WordPress code snippet can be to exclude any specific post type from the WordPress search results. In this example, In this example, I’m using events custom post type, you simply replace events with your required custom post type and use this code snippet in your WordPress website.

//Exclude posts from search
add_action( 'init', 'exclude_cpt_from_search', 99 );
function exclude_cpt_from_search() {
    global $wp_post_types;
	$wp_post_types['events']->exclude_from_search = true;
Need a helping hand in fixing your website issues?

If you are facing any problems in implementing these code snippets and tutorials, you can hire us to fix your website issues.

Hire Us