1
votes

classic wordpress structure

wpdb_term_relationships table

object_id

term_taxonomy_id

I want to remove term_taxonomy_id = 3 from all posts that have term_taxonomy_id 1424 but exclude the ones that also have term_taxonomy_id 3152.

query that does not work:

DELETE FROM wpdb_term_relationships WHERE term_taxonomy_id = 3
WHERE ID IN (
  SELECT object_id 
  FROM wpdb_term_relationships 
  WHERE term_taxonomy_id = 1424
)
AND ID NOT IN (
  SELECT object_id 
  FROM wpdb_term_relationships 
  WHERE term_taxonomy_id = 3152
)

what am i doing wrong?

1

1 Answers

0
votes

Firstly, I hope you're backing up your database before you're running any new queries, or running them on a testing database and not your live site's. You definitely shouldn't blindly trust us (especially me) to know exactly how it'll work in your specific case. Secondly, this might be better to ask over in the WordPress stack exchange suggested in the WordPress tag, just because people over there will likely be more familiar with the classic WordPress database structure.

As for your code, it looks like it would be much easier to just tack on the other conditions to your first WHERE statement.

DELETE FROM wpdb_term_relationships 
WHERE term_taxonomy_id = 3
 AND term_taxonomy_id = 1424
 AND term_taxonomy_id <> 3152 --or != 1352
;