I’m not sure about this, but the way the tables are setup, WordPress can re-use the same terms
record (with the same term_id
) within different taxonomies. That is, if you have a post_tag
called favorites
and you also have a category
called favorites
, it can well be that WP uses the same terms
record for both and only diversifies them via the term_taxonomy_id
table. There’s obvious advantages to that.
So…if your query above finds the term as part of the query and deletes it, then if it is also used as a category, it disappears, too, because the term record is gone and the relationship cannot be drawn anymore.
As said, i haven’t verified that WP is actually doing it that way, but it would explain your phenomenon.
Solution would be to only delete term if its id only appears in tt.term_id
once. I’d recommend breaking up the query for that.