Forums

Database problems: Syncing tables (2 posts)

  1. wpdevnb
    Member
    Posted 1 month ago #

    I hope you can help.

    I have two wordpress databases. Database A has all posts prior to today and database B has todays posts up until 6pm today, database A took over again after 6pm.

    My problem is how to get todays posts from database B into database A without getting duplicate entires. I figured i can reinsert database B's post using new ID's for the posts but I'm not sure what tables posts depend on. Can someone clarify and correct my script so far?

    My script so far:

    `$post_id = '19593';//19558

    $old_posts = $wpdb->get_results("SELECT * FROM DatabaseBposts WHERE ID > '$post_id'");

    echo '<h1>'.count($old_posts).' posts need to be moved</h1>';

    foreach($old_posts as $post)
    {
    // Get post meta
    $old_postmeta = $wpdb->get_results("SELECT * FROM DatabaseB_postmeta WHERE post_id = '".$post->ID."'");

    echo '<h2>'.count($old_postmeta).' postmeta need to be moved for '.$post->ID.'</h2>';

    $sql = $wpdb->prepare("INSERT INTO DatabaseA_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, post_category) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", $post->post_author, $post->post_date, $post->post_date_gmt, $post->post_content, $post->post_title, $post->post_excerpt, $post->post_status, $post->comment_status, $post->ping_status, $post->post_password, $post->post_name, $post->to_ping, $post->pinged, $post->post_modified, $post->post_modified_gmt, $post->post_content_filtered, $post->post_parent, $post->guid, $post->menu_order, $post->post_type, $post->post_mime_type, $post->comment_count, $post->post_category);
    $new_post = $wpdb->query($sql);
    echo $sql."\n";

    $new_post_id = $wpdb->insert_id;

    foreach($old_postmeta as $postmeta)
    {
    $sql = $wpdb->prepare("INSERT INTO DatabaseA_postmeta (post_id, meta_key, meta_value) VALUES (%s,%s,%s)",$new_post_id, $postmeta->meta_key, $postmeta->meta_value);
    $wpdb->query($sql);
    echo $sql."\n";
    }

    }

    Thanks

  2. MichaelH
    moderator
    Posted 1 month ago #

    1. Backup database A and Database B!!
    2. Make sure you did step 1
    3. Make your wp-config.php point to Database B and do Tools->Export.
    4. Make your wp-config.php point to Database A and do Tools->Import->WordPress using file from step 3.

Reply

You must log in to post.

About this Topic

Tags