How to not display tags with less than X posts

Is there a way to avoid displaying tags which have less than X Posts?

the answer here is close Remove from Google Tags with less than 2 posts

however it suggests to send a respose header to google. I would ideally like to NOT have these tags in my sitemap so that google does not index these pages which are basically duplicates of the original post with a different tag, not a good thing for SEO!

Topic conditional-tags wp-query tags Wordpress

Category Web


Tags don't create duplicate posts, so the likelihood of them hurting your SEO by appearing in a sitemap are slim.

You can add a filter to the return of the taxonomies sitemap to remove tags. This is just a quick attempt I threw together. I haven't tested it but it should provide a basis to work from at the very least.

function remove_low_tags($taxonomies) {
   //create a new array of tags with count above 2
   array $allowed_tags;
   //loop through current array of tags and add them to $allowed_tags if they are > 2
   foreach ($taxonomies as $tags) {
      if (count($tags) > 2) {
         $allowed_tags[] = $tags;
      }
   }
   return $allowed_tags;
}
add_filter('wp_sitemaps_taxonomies', 'remove_low_tags');



About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.