Hey Woo customizers! Today is episode #3 of my “18 snippets in 18 days” challenge.

Until March 14th (which I just found out it’s Albert Einstein birthday), I will publish 1 snippet per day to improve your WC skills and snippet library. Please make sure to share on social media and leave comments to support my chellenge!

In today’s episode we take a look at the WooCommerce shortcodes output and specifically how to exclude a specific category from all of those shortcodes.

WooCommerce: exclude category from shortcodes
WooCommerce: exclude category from shortcodes WooCommerce: exclude category from shortcodes

Exclude Category from All Products Shortcode

Let’s say we want to remove the category “black” (see above example) from every shortcode. The snippet to place in your functions.php is very simple 🙂


/**
 * @snippet       Exclude Category from All Products Shortcode
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=19860
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 2.5.2
 */

add_filter( 'woocommerce_shortcode_products_query' , 'bbloomer_exclude_cat_shortcodes');

function bbloomer_exclude_cat_shortcodes($query_args){

    $query_args['tax_query'] =  array(array( 
            'taxonomy' => 'product_cat', 
            'field' => 'slug', 
            'terms' => array('black'), // Don't display products from this category
            'operator' => 'NOT IN' 
        )); 

    return $query_args;
}

Share:

Leave a Reply