I have this function

$tags = get_tags( array('exclude' => 11, 12) )

Which excludes specific tags 11 and 12. But without manually having to add each tag ID I don't want shown, how do I exclude the category that these tags are coming from?

  • Sorry, I really don't get it. Could you maybe rephrase your problem somehow? – leymannx May 1 at 17:47
  • note that asking the database to exclude the tag for you is going to be very expensive/slow. It's much easier, and significantly faster, to ask for all the tags then do an if statement to skip over those 2 tags – Tom J Nowell May 1 at 17:53
  • Sorry, I'm having a hard time wording this question: Rather than specifying and manually adding tag id's to exclude from a category, is there a way to just add a category so that all tags from that category aren't shown? I hope that makes more sense... – harshclimate May 1 at 18:08
  • You need a way to mark tags in admin, which will be automatically hidded on front-end, right? – anton May 1 at 18:20
  • Tags don't belong to categories. They're just attached to posts. So no, here isn't a way to provide a category to exclude because tags and categories aren't related ike that. If you have multiple sets of tags that you use differently for posts in different categories, then you'd be better off created a separate taxonomy. – Jacob Peattie May 2 at 9:28

Since this question is too hard to ask, I'm going to use custom taxonomies. Thanks for your help so far, everyone.


You could get all of the term IDs for the badcategory and use those in your exclude filter:

$bad_terms = get_terms( [ 'taxonomy' => 'badcategory', 'fields' => 'ids' ] );
$good_tags = get_tags( [ 'exclude' => $bad_terms ] );
  • You've misunderstood the question. This code won't do anything. Tags can't belong to another taxonomy, so excluding specific categories from get_tags() doesn't make any sense. They're already not going to be included. – Jacob Peattie May 2 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.