1

wordpress always store revision after call wp_update_post. How to get the post revision ID after call wp_update_post?

Thanks

2 Answers 2

1

You can use wp_get_post_revisions.

<?php
// some logic here

$post_id = wp_update_post(/* ... */);
$latest_revision = array_shift(wp_get_post_revisions($post_id));

if ($latest_revision) {
   // do stuff with the latest revision
   // $latest_revision->ID will contain the latest revision
}
3
  • I think you meant array_shift, and NOT array_unshift. So it would be: $latest_revision = array_shift(wp_get_post_revisions($post_id));
    – Caio Mar
    Jul 17, 2015 at 23:18
  • Good catch, changed! Jul 18, 2015 at 1:07
  • 1
    Note that array_shift() rebuilds the array index. It is rather slow. Since you don't need that array later, you could just use current().
    – fuxia
    Jul 18, 2015 at 1:39
1

A slightly easier approach if you only need the current revision is to use the function added in WP5.6 wp_get_latest_revision_id_and_total_count() https://developer.wordpress.org/reference/functions/wp_get_latest_revision_id_and_total_count/. It's kind of unfortunate there isn't just a "get_latest_revision_id" function but this one will return it, along with the total count. It's easier than looping over all revisions which we had to do prior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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