2

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?

2

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!

2
  • Thanks! wow, you really worked it out all the way :) you saved me so much time!
    – Ben
    Nov 1 '16 at 11:08
  • You're welcome, glad to hear it worked for you @Ben
    – birgire
    Nov 2 '16 at 8:21
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.