Exclude custom taxonomy from search results and archive pages

Copy the following snippet and paste it into the code of your theme, preferably in the functions.php file:

    /* Exclude a Category from Search Results */

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

    if( $query->is_admin )
    return $query;

    if( $query->is_search ) {
    $query->set( 'category__not_in' , array( 30 ) ); // Cat ID
    }
    return $query;
    }

Leave a Comment