Find latest/published version of post using wp-cli

Answering my own question, the current revision has these properties as compared to revisions:

  • The post_status will be “publish” not “inherit”.
  • The post_parent will be 0
  • The post_type will be “post” not “revision”.

Additionally, the ID of the current revision doesn’t change when you update the post; instead, the older version is given a new ID.

Example:

  • I created a post called “test post” with content “this is revision one.”. It was assigned ID 28623. When I did “wp post get 28623”, the interesting part of the output was:

ID      28623
post_date       2017-12-13 16:26:02
post_content    this is revision one.
post_title      test post
post_status     publish
comment_status  open
ping_status     open
post_name       test-post
post_modified   2017-12-13 16:26:02
post_parent     0
post_type       post
  • I then edited the content to read “this is revision two” and did “wp post get 28623” again. The results this time:

ID      28623
post_date       2017-12-13 16:26:02
post_content    this is revision two.
post_title      test post
post_status     publish
comment_status  open
ping_status     open
post_name       test-post
post_modified   2017-12-13 16:28:47
post_parent     0
post_type       post

Notice that post id 28623 itself has changed fields, and the live version if still id 28623. The post_date remains the same but the post_modified has changed.

What happened to the old version? I did “wp post get 28624”. The results:


ID      28624
post_date       2017-12-13 16:26:02
post_content    this is revision one.
post_title      test post
post_status     inherit
comment_status  closed
ping_status     closed
post_name       28623-revision-v1
post_modified   2017-12-13 16:26:02
post_parent     28623
post_type       revision

The old version has been copied to a new row in the database. Note that there’s no guarantee that the revision will have the ID immediately following the original post’s id: wordpress will just use the next available id (it worked in my case because I didn’t make any posts between the post and its revision).

  • Additionally:

    • the post_status is now “inherit”, not “publish”

    • the post_type is “revision” not “post”

    • the post_parent is 28623, not 0

    • the post_name has changed to “28623-revision-v1” instead of “test-post”, and no longer references the title. If the original post is at “(something)/test-post”, you might be tempted to visit “(something)/28623-revision-v1” to see the revision. However, WordPress will yield a “page not found” error, since it doesn’t allow end users to see revisions.

    • the post_modified is still equal to the post_date, since this was the original version

    • the “comment_status” and “ping_status” are now closed, since it wouldn’t make sense to comment on or ping a revision. Of course, live posts can also have these both be “closed”, but, if you allow comments/pings on a post, that permission will automatically be removed for older revisions.

  • I then reverted back to the original version, and did “wp post get 28623” to get:


ID      28623
post_author     1
post_date       2017-12-13 16:26:02
post_content    this is revision one.
post_title      test post
post_status     publish
comment_status  open
ping_status     open
post_name       test-post
post_modified   2017-12-13 16:46:20
post_type       post

Notice that the post has now returned to the original version, with one exception: the post_modified date no longer matches the post_date. In other words, if you modify a post and then modify it back, that’s considered two modifications, not an undo of a modification (you can see this in the admin interface by noticing the “revisions” count is now 3, not 1 or 2).

Another way to see this is to do “wp post get 28624”:


ID      28624
post_date       2017-12-13 16:26:02
post_content    this is revision one.
post_title      test post
post_status     inherit
comment_status  closed
ping_status     closed
post_name       28623-revision-v1
post_modified   2017-12-13 16:26:02
post_parent     28623
post_type       revision

Notice that, even though we’ve reverted to version 1, a separate copy of version 1 exists as a revision. Version 2 was given a new id as we see by doing “wp post get 28625”.


ID      28625
post_date       2017-12-13 16:28:47
post_content    this is revision two.
post_title      test post
post_status     inherit
comment_status  closed
ping_status     closed
post_name       28623-revision-v1
post_modified   2017-12-13 16:28:47
post_modified_gmt       2017-12-13 16:28:47
post_parent     28623
post_type       revision