最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - batch adding posts to a category based on matching regex for title with SQL

programmeradmin1浏览0评论

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'

cheers

Ben

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'

cheers

Ben

Share Improve this question edited Jun 9, 2019 at 2:46 Benedict Harris asked Jun 8, 2019 at 10:47 Benedict HarrisBenedict Harris 1213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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?

发布评论

评论列表(0)

  1. 暂无评论