revert one revision of a post progmattically via code?

I would have thought a simple question but I can’t find a function to do this in the WordPress API.

I just want to revert to one previous revision as soon as a user updates a post. This would allow admins/editors to review the latest non live revision whilst the old one gives the appearance of staying live. The editor/admin would then switch the post to the newest revision when they are happy with it.

Solutions Collecting From Web of "revert one revision of a post progmattically via code?"

So via lots of head banging and experimentation I managed to get a solution to letting editors/admins review the post whilst a previous one stays live is rather convoluted but does work.

The key thing I did was to create a new post status which I call awaiting. In the admin it appears as “changes awaiting approval”.

// Define new awaiting status
function my_custom_post_status(){
register_post_status( 'awaiting', array(
    'label'                     => _x( 'Changes Awaiting Approval', 'custom-post-type' ),
    'public'                    => true,
    'exclude_from_search'       => false,
    'show_in_admin_all_list'    => true,
    'show_in_admin_status_list' => true,
    'label_count'               => _n_noop( 'Changes Awaiting Approval <span class="count">(%s)</span>', 'Changes Awaiting Approval <span class="count">(%s)</span>' ),
   ) );
 }
add_action( 'init', 'my_custom_post_status' );

Critically this is a public status type.

I then force updates by specific users to change the post status to this new status via a hook at the end of the update post process…

function published_to_pending( $post_id ) {
    global $post, $current_user, $wp_meta_boxes ;

    if ( !is_object( $post ) )
        $post = get_post( $post_id );
    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
    // check for specific user that needs this publish to pending. otherwise exit
    if (get_current_user_role()!="custom-user-role"){
        return;
    }
    if ( $post->post_status=="publish" ) {
        global $wpdb;
        $result = $wpdb->update(
            $wpdb->posts,
            array( 'post_status' => "awaiting" ),
            array( 'ID' => $post_id )
        );
    }
}
add_action( 'post_updated', 'published_to_pending', 13, 2 );

function get_current_user_role() {
    global $wp_roles;
    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    $role = array_shift($roles);

return $role;
}

Note that a very common way of updating the post status via the “save_post” hook was no good for me. It’s because I realised using this hook doesn’t save meta data correctly connected to the post and this ruined my AdvancedCustomField properties.

Finally in the front end on the single.php page for viewing my entries, I do a check for the post status and if it is ==”awaiting” I then do a manual $wpdb query to obtain the previous revision of the post and its related meta properties. I hope this is useful to others.

I doubt it’s easy to do what you want and force editors to automatically view one revision whilst another is published (there may be a plugin), but here’s some standalone code for identifying revisions and reverting to a specific one:

# get all revisions for post ID 1 in an array (keys = ID, val = full WP_Post object)
$revisions = wp_get_post_revisions(1);
# they're returned in reverse order (most recent first), so previous is 2nd array element
wp_restore_post_revision($revisions[1]->ID);

Note: there will only be one revision when a post is first published (unless the user saved a draft.)

Documentation: wp_restore_post_revision