WordPress – 在SQL中将术语设置为所有帖子

huangapple go评论58阅读模式
英文:

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! WordPress – 在SQL中将术语设置为所有帖子


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! WordPress – 在SQL中将术语设置为所有帖子


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>



huangapple
  • 本文由 发表于 2023年4月13日 20:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定