当前接受的帖子也超出了范围。由于它在wp\\u term\\u关系上留下了JOIN,而不知道第一个JOIN可能是一个Category或Post\\u标记,因此它给出了误报,从而影响了可能没有Category的帖子。
受当前公认答案的启发,将“37”改为您实际想要的类别:
INSERT IGNORE INTO wp_term_relationships
(object_id, term_taxonomy_id, term_order)
SELECT p.ID as `object_id`, 37 as `term_taxonomy_id`, 0 as `term_order`
FROM wp_posts p
WHERE p.post_type="post"
AND p.post_status=\'publish\'
AND NOT EXISTS
(
SELECT *
FROM wp_term_relationships rel
JOIN wp_term_taxonomy tax
ON tax.term_taxonomy_id = rel.term_taxonomy_id
AND tax.taxonomy = \'category\'
JOIN wp_terms term
ON term.term_id = tax.term_id
WHERE p.ID = rel.object_id
)