I am trying to shift post revisions to a custom table and when the edit page is loaded then I transfer back the posts of those specific post back to wp_posts table
but I am encountering a problem
all the posts are transferring back and forth but on the revisions page I can see the last revision but if I scroll to the previous revisions it shows a error
"Sorry, something went wrong. The requested comparison could not be loaded."
I tried to find the error in cosole, debug.log and even in php logs but it does not show any kind of error
As I have made sure that the ID's of the post revisions and the parent ID's are the same I expect that they should be properly managed from the revisions page
function is_table_exist($table_name) {
global $wpdb;
$sql = "SHOW TABLES LIKE '" . esc_sql($wpdb->prefix . $table_name) . "'";
$table = $wpdb->get_var($sql);
return $table == $wpdb->prefix . $table_name;
}
if (is_table_exist('posts_revisions')) {
// Table exists
echo "Table exists!";
function has_post_revisions() {
global $wpdb;
$revision_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'revision'" );
return $revision_count > 0;
}
if ( has_post_revisions() ) {
echo 'There are revisions in the posts table.';
function transfer_post_revisions_to_custom_table() {
global $wpdb;
// Get revisions from the posts table
$revisions = $wpdb->get_results(
"SELECT * FROM $wpdb->posts WHERE post_type = 'revision'"
);
if ($revisions) {
foreach ($revisions as $revision) {
// Check if the revision already exists in the custom table
$existing_revision = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}posts_revisions WHERE id = %d",
$revision->ID
)
);
// Insert the revision into the custom table if it doesn't already exist
if (!$existing_revision) {
$wpdb->insert(
$wpdb->prefix . 'posts_revisions',
array(
'id' => $revision->ID,
'post_author' => $revision->post_author,
'post_date' => $revision->post_date,
'post_date_gmt' => $revision->post_date_gmt,
'post_content' => $revision->post_content,
'post_title' => $revision->post_title,
'post_excerpt' => $revision->post_excerpt,
'post_status' => $revision->post_status,
'comment_status' => $revision->comment_status,
'ping_status' => $revision->ping_status,
'post_password' => $revision->post_password,
'post_name' => $revision->post_name,
'to_ping' => $revision->to_ping,
'pinged' => $revision->pinged,
'post_modified' => $revision->post_modified,
'post_modified_gmt' => $revision->post_modified_gmt,
'post_content_filtered' => $revision->post_content_filtered,
'post_parent' => $revision->post_parent,
'guid' => $revision->guid,
'menu_order' => $revision->menu_order,
'post_type' => $revision->post_type,
'post_mime_type' => $revision->post_mime_type,
'comment_count' => $revision->comment_count
)
);
}
}
// Delete revisions from the posts table
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->posts WHERE post_type = 'revision'"
)
);
}
}
// Run the function
// transfer_post_revisions_to_custom_table();
} else {
echo 'There are no revisions in the posts table.';
}
} else {
// Table does not exist
echo "Table does not exist!";
function create_posts_revisions_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'posts_revisions';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
post_author bigint(20) unsigned NOT NULL default '0',
post_date datetime NOT NULL default '0000-00-00 00:00:00',
post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
post_content longtext NOT NULL,
post_title text NOT NULL,
post_excerpt text NOT NULL,
post_status varchar(20) NOT NULL default 'publish',
comment_status varchar(20) NOT NULL default 'open',
ping_status varchar(20) NOT NULL default 'open',
post_password varchar(255) NOT NULL default '',
post_name varchar(200) NOT NULL default '',
to_ping text NOT NULL,
pinged text NOT NULL,
post_modified datetime NOT NULL default '0000-00-00 00:00:00',
post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00',
post_content_filtered longtext NOT NULL,
post_parent bigint(20) unsigned NOT NULL default '0',
guid varchar(255) NOT NULL default '',
menu_order int(11) NOT NULL default '0',
post_type varchar(20) NOT NULL default 'post',
post_mime_type varchar(100) NOT NULL default '',
comment_count bigint(20) NOT NULL default '0',
PRIMARY KEY (id),
KEY post_name (post_name(191)),
KEY type_status_date (post_type,post_status,post_date,ID),
KEY post_parent (post_parent),
KEY post_author (post_author)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
// Hook to run the function when WordPress initializes
add_action( 'init', 'create_posts_revisions_table' );
}
function is_admin_post_edit_page() {
if ( isset($_GET['action']) ) {
// Check if the current screen is the post edit screen
if ($_GET['action'] == "edit" ) {
return true;
}
}
return false;
}
if ( is_admin_post_edit_page() ) {
// Do something if it's the post edit page
echo "is edit page";
$this_post_id = $_GET['post'];
global $wpdb;
$table_name = $wpdb->prefix . "posts_revisions";
$revisionArray = $wpdb->get_results("SELECT * FROM $table_name WHERE post_parent = '$this_post_id'");
// print_r($revisionArray);
foreach ($revisionArray as $revision) {
$wpdb->insert(
$wpdb->posts,
array(
'ID' => $revision->id, // Use the ID from the revision as the post ID
'post_author' => $revision->post_author,
'post_date' => $revision->post_date,
'post_date_gmt' => $revision->post_date_gmt,
'post_content' => $revision->post_content,
'post_title' => $revision->post_title,
'post_excerpt' => $revision->post_excerpt,
'post_status' => $revision->post_status,
'comment_status' => $revision->comment_status,
'ping_status' => $revision->ping_status,
'post_password' => $revision->post_password,
'post_name' => $revision->post_name,
'to_ping' => $revision->to_ping,
'pinged' => $revision->pinged,
'post_modified' => $revision->post_modified,
'post_modified_gmt' => $revision->post_modified_gmt,
'post_content_filtered' => $revision->post_content_filtered,
'post_parent' => $revision->post_parent,
'guid' => $revision->guid,
'menu_order' => $revision->menu_order,
'post_type' => $revision->post_type,
'post_mime_type' => $revision->post_mime_type,
'comment_count' => $revision->comment_count
)
);
}
} else {
// Do something else if it's not the post edit page
echo "is not edit page";
}