Exclude Products Category from loops shortcode Woocommerce

Viewed 113

I am trying to exclude or remove a 'custom' products category, it works fine on the shop page also, in homepage when I tried to exclude from shortcodes, products of that category doesn't show but in another shortcode, on the same page (Homepage) it gives me some problems when I try in another container to only show products of another category it doesn't work it shows me all products from 'NEWEST', not only from that certain category I filtered it, can someone help me how to fix it? Thanks

add_action( 'woocommerce_shortcode_products_query' , 'exclude_cat_shortcodes');
 
function exclude_cat_shortcodes($query_args){
 
    $query_args['tax_query'] =  array(array( 
            'taxonomy' => 'product_cat', 
            'field' => 'slug', 
            'terms' => 'custom', // slug category that I want to exclude
            'operator' => 'NOT IN'
        )); 
 
    return $query_args;
}
2 Answers

To prevent a tax_query overrite do this

$query_args['tax_query'][] = array( 
    'taxonomy' => 'product_cat', 
    'field' => 'slug', 
    'terms' => ['custom'], // slug category that I want to exclude
    'operator' => 'NOT IN'
); 

For excluding categories or post-types from the query try placing the slug category within an array? This is to allow multiple categories/post-types to be excluded.

Change:

'terms' => 'custom'

to:

'terms' => ['custom']

Related