Exclude a term of a taxonomy with a custom post type in a search

The problem with your approach is that woocommerces product category is a custom taxonomy called product_cat. But with cat you are addressing the built-in category. Taxonomies can be addressed with a tax query, simplified example below:

function wpse188669_pre_get_posts( $query ) {
    if ( 
        ! is_admin() 
        && $query->is_main_query() 
        && $query->is_search() 
    ) {
        $query->set( 'post_type', array( 'product' ) );
        // set your parameters according to
        // https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
        $tax_query = array(
            array(
                // likely what you are after
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => 'sold-gallery',
                'operator' => 'NOT IN',
            ),
        );
        $query->set( 'tax_query', $tax_query );
  }
}
add_action( 'pre_get_posts', 'wpse188669_pre_get_posts' );

Leave a Comment