Exclude Custom Post Types from Search Results

If you’re working with a number of post types and only want certain post types returned in your WordPress search results here is a neat function you can add to your theme’s function.php file (or plugin).

Fine tuning your WordPress search results is a good feature to include to help assist your users in finding what they are searching. WordPress’ search results by default will return a lot of content you may not want displayed. For my case, I only needed to return the standard posts type rather than all of my custom post types.

Here’s the function that helped me do that:

function excludePages($query) {

if ($query->is_search) {

	$query->set('post_type', 'post');

}
	return $query;

}

add_filter('pre_get_posts','excludePages');

Function Explained

We are using the pre_get_posts filter only on the search results page to only return the standard post type. You can customize the function above to return one or many post types.

Here’s another example I found over at WPRecipes that demonstrates how to exclude posts or pages from search results:

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('cat','8,15');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');

I hope this helps you further better your site’s search results! Let me know how it works for you in the comments below…

16 thoughts on “Exclude Custom Post Types from Search Results

  1. I was hoping to find a function like this. Super cool and makes search much more useful when you’re posting lots of custom posts and pages that you don’t want to be returned in results.

  2. I was hoping to find a function like this. Super cool and makes search much more useful when you’re posting lots of custom posts and pages that you don’t want to be returned in results.

  3. hello, i just wanna ask if you could provide a code that will trim and show only the text beetween theText Here .. also, a code that will show the text which has a beginning of a word ‘Price’ e.g: post text… Price: $23.99…text text… the output will be, $23.99 thanks in advance, cheers!

  4. hello, i just wanna ask if you could provide a code that will trim and show only the text beetween theText Here .. also, a code that will show the text which has a beginning of a word ‘Price’ e.g: post text… Price: $23.99…text text… the output will be, $23.99 thanks in advance, cheers!

    1. After a few tests, I’ve noticed that the function works great with just one custom post type, but if you add an array with two or more custom post types, it just filters through one of them. I’ve found this other hook that does the job: function customSearchFilter($query) { if ( !$query->is_admin && $query->is_search) { $query->set(‘post_type’, array(‘events’, ‘recipes’,’post’) ); } return $query; } add_filter( ‘pre_get_posts’, ‘customSearchFilter’ ); Greetings!

  5. The best way is : Use exclude_from_search = true with post type register args. https://codex.wordpress.org/Fu… If you can not edit the code for the post_type register then you can simply hack the $args by using hook. add_action( ‘register_post_type_args’, function( $args, $post_type ) { if ( ‘your_post_type’ == $post_type ) { $args[‘exclude_from_search’] = true ; } return $args; }, 1, 2 );

Leave a Reply

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