英文:
WordPress - set term to all posts in sql
问题
UPDATE `wp_term_relationships` SET `term_taxonomy_id` = '222' WHERE `object_id` IN (SELECT ID FROM `wp_posts` WHERE `post_type` LIKE 'xxx');
英文:
I have some post type, let's say xxx with custom taxonomy. I want to set some term to all posts from this post type, in phpmyadmin.
So this is how I can get all posts ids:
SELECT ID FROM `wp_posts` WHERE `post_type` LIKE 'xxx';
and this is how I can set term id 222 to post id 111.
INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES ('111', '222', '0');
But how can I set term id 222 to all selected posts? Is there something like foreach in sql?
I don't want to do it in php, because I need to execute it only once, not every time wordpress loads. I also don't want to do it by logging to wp admin.
答案1
得分: 0
好的,我有答案。谢谢,Chat GPT!
CREATE PROCEDURE set_terms_to_posts()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE post_id INT;
DECLARE cur CURSOR FOR SELECT ID FROM `wp_posts` WHERE `post_type` LIKE 'xxx';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO post_id;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES (post_id, 222, 0);
END LOOP;
CLOSE cur;
END//
DELIMITER ;
CALL set_terms_to_posts();
英文:
OK, I have the answer. Thank you Chat GPT!
CREATE PROCEDURE set_terms_to_posts()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE post_id INT;
DECLARE cur CURSOR FOR SELECT ID FROM `wp_posts` WHERE `post_type` LIKE 'xxx';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO post_id;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES (post_id, 222, 0);
END LOOP;
CLOSE cur;
END//
DELIMITER ;
CALL set_terms_to_posts();
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论