How to remove products with specific tag from recent product shortcode in Woocommerce?

By default woocomemrce comes with a number of shortcodes which could enable us to achieve some of the functionalities without any coding.

One of those shortcodes is recent products shortcode. This shortcode when placed on a page or post will display recent products of the shop on that page.

In this tutorial we’ll learn how to remove products with specific tags from listing by this short code.

So first of all create a page or post and place the shortcode [recent_products] in that.

woocommerce

Now from front end go to the newly created page and you’ll see recent products listed there.

woocommerce

Now let’s say we want to remove the products with tag ‘xyz’ from this listing.

To do this add the following lines of code at the end of your theme’s functions.php file:

add_filter('woocommerce_shortcode_products_query', 'removeAudioTags');

   function removeAudioTags($args){

       $args['tax_query'] = array(array(

           'taxonomy' => 'product_tag',

           'field' => 'slug',

           'terms' => array('xyz'),

           'operator' => 'NOT IN'

   ));

   return $args;

}

You can access functions.php file here:

woocommerce

Save file after placing code.

Now go to a product and add a tag ‘xyz’ there.

woocommerce

Save the product after adding tag.

Now again go the page where shortcode has been placed from front end and you’ll see that specific product being removed from that listing page.

woocommerce

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

Leave a Comment

Your email address will not be published. Required fields are marked *