I am adding location categories to posts based on keywords in title (with regex), I got it working once, so code is fine the first time, but now get SQL Error (1062) Duplicate entry '4419-268'

so I assume its because some fields already are filled because it worked once...what is the correct code for updating categories even when some might already have same one? my regex catches some already with that category

here is my current code

INSERT INTO wp_term_relationships (term_taxonomy_id, object_id)
SELECT '268',  ID
FROM  `wp_posts` WHERE (post_title REGEXP '€|E\\.U\\.|Euro|Europe|Germany|Deutschland|Berlin|Munich|München' 
OR post_content REGEXP '€|E\\.U\\.|Euro|Europe|Germany|Deutschland|Berlin|Munich|München')
AND post_type = 'post'



1 Answer 1


figured it out. I spent literally days trying different code and the answer was under my nose. Lack of sleep!

replace "INSERT" with "REPLACE"

any tips for removing a category?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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