4

Is there a way to get the revision count number of a post? I've seen how to get the revisions but I don't want to get the revisions just the count.

5

You can use wp_get_post_revisions ( int|WP_Post $post_id, $args = null ). It returns an array of revisions for the passed post ID or an empty array if a passed post does not have any revisions.

Note that the $args parameter accepts all parameters valid for WP_Query

$q = wp_get_post_revisions( 513 );
echo count( $q );
?><pre><?php var_dump($q); ?></pre><?php    

EDIT

From your comments about custom fields, custom fields does not trigger the post revision system.

I'm not going to discuss how the revision system works, but in short, when a post is updated, the old and new values of the WP_Post is compared. There are a specific action for that, post_updated. If these value don't match, a revision is created (if revisions are enabled).

Now, custom fields are not part of the WP_Post object, there for, whenever you just update a custom field, a post revision will not be created as the post object remains the same. Also, custom fields also don't have a revision system, so old values are completely overwritten by new values

  • Strange. I'm not showing any revisions. Are revisions only created when the document is published? – 1.21 gigawatts Oct 13 '15 at 4:32
  • Interesting... It won't save revisions OR show the revisions panel UNLESS the document body changes. That seems like a good idea but I've been updating the custom metadata and pulling my hair out trying to figure out why no revisions were being saved!!! – 1.21 gigawatts Oct 13 '15 at 4:38
  • Revisions (if enabled) are created when a post is updated. Note that, by default, custom post types don't have revisions enabled. – Pieter Goosen Oct 13 '15 at 4:40
  • 1
    To be honest, the revision system is a really crappy one which I really dont like – Pieter Goosen Oct 13 '15 at 4:42
  • 2
    Please see my update. In short, custom field updates does not trigger revisions – Pieter Goosen Oct 13 '15 at 5:07

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.