Kimberly
Kimberly

Reputation: 21

woocommerce shortcodes filter to exclude category?

Is it possible to use shortcodes, best seller for example, but filter to exclude a category?

I understand how to use [best_selling_products per_page="12"] or [product_attribute attribute='color' filter='black']

but I need to filter out my free stuff section so that doesn't show on best sellers.

Update: I found exclude_categories="" but now I'm not sure what the category ID is. Is it the slug or name?

Upvotes: 2

Views: 6154

Answers (2)

sot
sot

Reputation: 45

You can use this shortcode [recent_products category="category_link" operator="NOT IN"]

Upvotes: 2

Maruti Mohanty
Maruti Mohanty

Reputation: 217

In the latest version of Woocommerce, it provides a filter woocommerce_shortcode_products_query which you can use to exclude a category from that page

You can use the below code in your theme's functions.php file to make it work.

But I would advice to do it using a Child Theme

function se_customize_product_shortcode( $args, $atts ) {
    if ( is_page( 'products' ) ) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => array( 'clothing', 'music' ),
                'operator' => 'NOT IN'
            )
       );
    }

    return $args;
}
add_filter( 'woocommerce_shortcode_products_query', 'se_customize_product_shortcode', 10, 2 );

You can use your respective is_page condition. For more details check out this blog Exclude a category from the woocommerce product shortcode

Upvotes: 3

Related Questions