Excluding categories in WordPress is a fairly simple endeavor, whether it be in the loop or in the search query.

However there are times when you have multiple different search forms in WordPress and you may need to manipulate only one of them.

In this post we will look specifically at how to exclude categories from a specific search form in WordPress.

For example you have two forms. One searches for all post types (post, page, etc.) and the other only searches for posts. We want the one that searches for posts only to exclude a couple of categories.

Video Tutorial

Written Tutorial

If you prefer not to watch the video or need more instructions, then continue reading.

Here is the code to do it:


add_filter( 'pre_get_posts' , 'exclude_cats_blog_search' );
function exclude_cats_blog_search( $query ) {

    if ( $query->is_search && $_GET['post_type'] == 'post') {
        $query->set( 'category__not_in' , array( 121, 203 ) ); //replace with your specific category IDs

If the query is search and the post type of our Get request form is a post, then exclude the categories in the array.

And that’s it. That is how you can exclude categories from a specific search form in WordPress

Hope this helps!


Travis Rodgers

Buy Me a Coffee at ko-fi.com

Hi, I'm the Travis in Travis.Media. I'm a self-taught software developer, blogger, and YouTuber, sharing everything I'm learning along the way.

Show comments