Skip to content

Instantly share code, notes, and snippets.

@cam5
Created February 24, 2014 02:17
Show Gist options
  • Save cam5/9180843 to your computer and use it in GitHub Desktop.
Save cam5/9180843 to your computer and use it in GitHub Desktop.
<?php
$id_map = array();
$update = array();
$posts = $db->query("SELECT * from wp_posts");
for ($count = 1; $count <= $posts->num_rows; $count++) {
$row = $posts->fetch_assoc();
$id_map[$row['ID']] = $count;
if ($row['post_type'] == 'post' && $row['post_title'] == '')
$update[$row['ID']] = $count;
$currentID = $row['ID'];
// Update post IDs sequentially
$db->query("UPDATE wp_posts set id = " . $count . " WHERE id = " . $currentID);
// Update their post_metas reference, and restore images
$db->query("UPDATE wp_postmeta set post_id = " . $id_map[$currentID] . " WHERE post_id = " . $currentID);
$db->query("UPDATE wp_postmeta set meta_value = " . $id_map[$currentID] . " WHERE meta_value = " . $currentID . " AND meta_key = '_thumbnail_id'");
// Update the row's children.
if ( $db->query("SELECT post_parent from wp_posts where post_parent = $currentID")->fetch_assoc() )
$db->query("UPDATE wp_posts set post_parent = " . $id_map[$currentID] . " WHERE post_parent = " . $currentID);
// If it have comment!!
if ( $row['comment_count'] != 0 )
$db->query("UPDATE wp_comments set comment_post_ID = " . $id_map[$currentID] . " WHERE comment_post_ID = " . $currentID);
// If it's an MM Core Page
if ( $db->query("SELECT page_id from mm_core_pages where page_id = $currentID")->fetch_assoc() )
$db->query("UPDATE mm_core_pages set page_id = " . $id_map[$currentID] . " WHERE page_id = " . $currentID);
// If it's an MM 'access' page
if ( $db->query("SELECT post_id from mm_posts_access where post_id = $currentID")->fetch_assoc() )
$db->query("UPDATE mm_posts_access set post_id = " . $id_map[$currentID] . " WHERE post_id = " . $currentID);
// If it has buddyPress activity
if ( $bp = $db->query("SELECT * from wp_bp_activity where item_id = $currentID")->fetch_assoc() ) {
$db->query("UPDATE wp_bp_activity
set item_id = " . $id_map[$currentID] .
", primary_link = '" . preg_replace('/' . $currentID . '$/', $id_map[$currentID], $bp['primary_link']) . "'" .
" WHERE item_id = $currentID");
}
// If it's secondary buddyPress activity
if ( $db->query("SELECT id from wp_bp_activity where secondary_item_id = $currentID")->fetch_assoc() )
$db->query("UPDATE wp_bp_activity set secondary_item_id = " . $id_map[$currentID] . " WHERE secondary_item_id = $currentID");
// If there's a buddyPress Group
if ( 'forum' === $row['post_type'] )
if ( $bp = $db->query("SELECT * from wp_bp_groups_groupmeta WHERE meta_value LIKE '%$currentID%'")->fetch_assoc() ) {
$db->query("UPDATE wp_bp_groups_groupmeta
set meta_value = '" . preg_replace("/$currentID;}/", $id_map[$currentID] . ";} ", $bp['meta_value']) . "'" .
" WHERE meta_value LIKE '%$currentID%'");
$db->query("UPDATE wp_bp_groups_groupmeta
set meta_key = '" . preg_replace("/$currentID/", $id_map[$currentID] . ";} ", $bp['meta_key']) . "'" .
" WHERE meta_key LIKE '%$currentID%'");
}
if (in_array($row['post_type'], array('topic', 'reply', 'forum')))
$db->query("UPDATE wp_postmeta
set meta_value = " . $id_map[$currentID] . "
WHERE meta_value = $currentID");
}
echo "<pre>" . json_encode($update) . "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment