Skip to content

Instantly share code, notes, and snippets.

@wh1tew0lf
Last active February 8, 2017 12:48
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 wh1tew0lf/0dd40cbce8e9ca4de0ec5dc00f67700d to your computer and use it in GitHub Desktop.
Save wh1tew0lf/0dd40cbce8e9ca4de0ec5dc00f67700d to your computer and use it in GitHub Desktop.
Copy data from one wordpress database to another (tables wp_posts and wp_postmeta)
-- Don't forget chech base_product at wp_postmeta
DELETE FROM dest_db.wp_terms
WHERE exists(SELECT * FROM dest_db.wp_term_taxonomy
WHERE taxonomy='wpc-template-cat' AND wp_term_taxonomy.term_id=wp_terms.term_id);
DELETE FROM dest_db.wp_term_relationships
WHERE term_taxonomy_id IN (SELECT wp_term_taxonomy.term_taxonomy_id FROM dest_db.wp_term_taxonomy
WHERE taxonomy='wpc-template-cat');
DELETE FROM dest_db.wp_term_taxonomy
WHERE taxonomy='wpc-template-cat';
DROP PROCEDURE IF EXISTS copy_taxonomy;
CREATE PROCEDURE copy_taxonomy()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE old_term_id INT;
DECLARE new_term_id INT;
DECLARE cur_term CURSOR FOR SELECT term_id FROM source_db.wp_terms
WHERE exists(SELECT * FROM source_db.wp_term_taxonomy
WHERE taxonomy='wpc-template-cat' AND wp_term_taxonomy.term_id=wp_terms.term_id);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur_term;
read_loop: LOOP
FETCH cur_term INTO old_term_id;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO dest_db.wp_terms (name, slug, term_group, term_order)
SELECT name, slug, term_group, term_order FROM source_db.wp_terms WHERE wp_terms.term_id=old_term_id;
SET @new_term_id=LAST_INSERT_ID();
INSERT INTO dest_db.wp_term_taxonomy (term_id, taxonomy, description, parent, count)
SELECT @new_term_id, taxonomy, description, parent, count FROM source_db.wp_term_taxonomy
WHERE wp_term_taxonomy.term_id=old_term_id;
END LOOP;
CLOSE cur_term;
END;
CALL copy_taxonomy();
DELETE FROM dest_db.wp_postmeta WHERE post_id IN (
SELECT ID FROM dest_db.wp_posts WHERE post_type='wpc-template'
);
DELETE FROM dest_db.wp_posts WHERE post_type='wpc-template';
DROP PROCEDURE IF EXISTS copy_data;
CREATE PROCEDURE copy_data()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE old_post_id INT;
DECLARE new_post_id INT;
DECLARE new_term_taxonomy_id INT;
DECLARE term_name VARCHAR(200);
DECLARE cur_post CURSOR FOR SELECT ID FROM source_db.wp_posts WHERE post_type='wpc-template';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur_post;
read_loop: LOOP
FETCH cur_post INTO old_post_id;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO dest_db.wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status,
comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified,
post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type,
post_mime_type, comment_count)
SELECT post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status,
comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified,
post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type,
post_mime_type, comment_count FROM source_db.wp_posts WHERE wp_posts.ID=old_post_id;
SET @new_post_id:=LAST_INSERT_ID();
INSERT INTO dest_db.wp_postmeta (post_id, meta_key, meta_value)
SELECT @new_post_id, meta_key, meta_value FROM source_db.wp_postmeta WHERE wp_postmeta.post_id=old_post_id;
SELECT t.name FROM source_db.wp_term_relationships r
LEFT JOIN source_db.wp_term_taxonomy tx ON tx.term_taxonomy_id=r.term_taxonomy_id
LEFT JOIN source_db.wp_terms t ON t.term_id=tx.term_id
WHERE r.object_id=old_post_id
LIMIT 1
INTO @term_name;
SELECT tx.term_taxonomy_id FROM dest_db.wp_terms t
LEFT JOIN dest_db.wp_term_taxonomy tx ON t.term_id=tx.term_id
WHERE t.name=@term_name
LIMIT 1 INTO @new_term_taxonomy_id;
INSERT INTO dest_db.wp_term_relationships (object_id, term_taxonomy_id, term_order)
VALUES (@new_post_id, @new_term_taxonomy_id, 0);
END LOOP;
CLOSE cur_post;
END;
CALL copy_data();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment