Last active
September 27, 2015 13:48
-
-
Save milero/1279590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delimiter // | |
CREATE PROCEDURE reattach_images() | |
BEGIN | |
DECLARE _attach_id BIGINT DEFAULT 0; | |
DECLARE _attach_guid VARCHAR(255); | |
DECLARE _parent_id BIGINT DEFAULT 0; | |
DECLARE _done INT DEFAULT 0; | |
BEGIN | |
DECLARE cur_attach CURSOR FOR SELECT ID, guid FROM wp_posts WHERE post_parent = 0 AND post_type = 'attachment'; | |
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = 1; | |
OPEN cur_attach; | |
REPEAT | |
FETCH cur_attach INTO _attach_id, _attach_guid; | |
IF (_done = 0) THEN | |
SELECT ID INTO _parent_id FROM wp_posts WHERE post_content LIKE CONCAT('%', _attach_guid, '%') AND post_type = 'post' LIMIT 0,1; | |
IF (_parent_id IS NOT NULL) THEN | |
UPDATE wp_posts SET post_parent = _parent_id WHERE ID = _attach_id AND post_type = 'attachment'; END IF; | |
SET _done = 0; | |
END IF; | |
UNTIL (_done = 1) | |
END REPEAT; | |
CLOSE cur_attach; | |
SET _done = 0; | |
END; | |
END // | |
CALL reattach_images(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment