I want to save two versions of my custom meta box data called “sidebar”. I want to compare a new and old version of this data later on, so that is why I want to save a copy of the current “sidebar” to a field called “sidebar_old”, just before saving a new version. Here’s my attempt at doing just that:
add_action('pre_post_update', 'content_old_save');
function content_old_save() {
global $post;
$sidebar_old = get_post_meta($post->ID, 'sidebar', true);
update_post_meta($post->ID, "sidebar_old", $sidebar_old); // current/old sidebar
}
This is how I save the new version:
add_action('save_post', 'sidebar_title_update');
function sidebar_title_update($post_id) {
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post->ID, "sidebar", $_POST["sidebar"]); // new sidebar
}
Thing is, only the new sidebar value is inserted into the database, to both “sidebar” and “sidebar_old”, so somehow the new value is already inserted into the db even before pre_post_update?
that could be caused by post revision being saved, and you should use
wp_insert_post_data
anytime you want to do something before the post is saved, here is an example plugin i just cooked up to test it and it looks like this: