Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milero/1279590 to your computer and use it in GitHub Desktop.
Save milero/1279590 to your computer and use it in GitHub Desktop.
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