Only Include Shop Page Products With More Than 1 Tag

The logic in this code, enables you to display products with at least 2 or more tags. Products with 1 or less tags, will not be included in your shop page loop and not shown on archives.

The code contains several functions which :

  • Adds a custom field to all products and checks how many tags they contain once only storing the value for use in other functions
  • Update the tag count when a product is saved of the number of tags has been changed
  • Filter shop page products by tag count and only display products with 1 or more tags

Other Post Types & Taxonomies

The code will also work for posts if you search and replace all instances of product with post.

The code will also work with product categories if you search and replace al instances of tag with cat.

Efficiency

  • Highly efficient code tested using data query monitor and returns green
  • Only uses WordPress API functions and not custom db queries

Demo

Shows the default shop page with all products displaying and then only products with more than 1 tag once the functions are saved in the child themes functions file.

Also shows products added or removed from the loop once updated with more than 1 tag or less than 2 tags.

Note: Checkbox added for testing only.

Was This Tutorial Helpful?