Is it possible to display previous post revision?

by Ben   Last Updated July 12, 2019 11:08 AM - source

I want to be able to loop through my posts, but for every post to this:

1) check if some custom field is true or false.

2) if is true just display the post's data (the_title, the_content...).

3) if is false display the same structure of data (the_title, the_content...) but with the pervious revision of this post.

Is it possible? and how?

Tags : loop revisions


Answers 2


Yes it's possible. You can access to post's revisions using wp_get_post_revisions($post_id) function. It returns array of post's revision, first element of an array is the same as current version of post so you should take second's element values.

Piotr Kliks
Piotr Kliks
October 30, 2016 18:05 PM

First we look into the wp_get_post_autosave function

It's informative to see how the core function wp_get_post_autosave() uses the wp_get_post_revisions() function.

It loops over all revisions from

$revisions = wp_get_post_revisions( 
    $post_id, 
    array( 
        'check_enabled' => false 
    ) 
);

and then uses:

foreach ( $revisions as $revision ) {
    if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
        if ( $user_id && $user_id != $revision->post_author )
            continue;

        return $revision;
    }
}

to return the first revision with a slug containing "{$post_id}-autosave" and where the $user_id possibly matches it's author.

Alternative

Now wp_get_post_revisions() is a wrapper for get_children(), so I wonder why it has to fetch all the revisions for the given post, before filtering out a single one. Why not try to fetch it directly, only what's needed? When we try e.g. the following (PHP 5.4+):

$revisions = wp_get_post_revisions(
    $post_id,
    [
        'offset'       => 1,    // Start from the previous change (ofset changed to offset)
        'posts_per_page'  => 1,    // Only a single revision
        'name'            => "{$post_id}-autosave-v1",
        'check_enabled'   => false,
    ]
);

then the posts_per_page will not have any effect. After playing around with this I managed to get the following to work with the posts_per_page argument:

$revisions = wp_get_post_revisions(
    $post_id,
    [
        'offset'           => 1,    // Start from the previous change
        'posts_per_page'  => 1,    // Only a single revision
        'post_name__in'   => [ "{$post_id}-autosave-v1" ],
        'check_enabled'   => false,
    ]
);

Get only the previous revision

Now we can adjust the above to only fetch the previous revision, i.e. that's not an auto-save:

$revisions = wp_get_post_revisions(
    $post_id,
    [
        'offset'           => 1,    // Start from the previous change
        'posts_per_page'  => 1,    // Only a single revision
        'post_name__in'   => [ "{$post_id}-revision-v1" ],
        'check_enabled'   => false,
    ]
);

Here we target the {$post_id}-revision-v1 slug.

Note the here we use the v1, but we could adjust that if needed later on.

Example

So to finally answer your question, here's a suggestion:

$show = get_post_meta( get_the_ID(), 'somekey', true );

if( $show )
{
    // Template part
    get_template_part( 'template-parts/content' );
}
else
{
    // Fetch the previous revision only
    $revisions = wp_get_post_revisions(
        get_the_ID(),
        [
            'offset'           => 1,    // Start from the previous change
            'posts_per_page'  => 1,    // Only a single revision
            'post_name__in'   => [ sprintf( "%d-revision-v1", get_the_ID() ) ],
            'check_enabled'   => false,
        ]
    );

    if( $revisions )
    {
        $post = reset( $revisions );
        setup_postdata( $post );

        // Template part
        get_template_part( 'template-parts/content' );

        wp_reset_postdata();
    }
    else
    {
         // Some fallback here
    }
}

Hopefully you can adjust it further to your needs!

birgire
birgire
October 31, 2016 00:48 AM

Related Questions




Disable revision access for a specific user role

Updated April 14, 2015 02:33 AM

Page edit auto-creates blank revision and editor

Updated February 23, 2017 02:08 AM

how to enable revisions for media(documents)

Updated October 02, 2017 21:08 PM