Returns the latest revision ID and count of revisions for a post.
Parameters
Source
function wp_get_latest_revision_id_and_total_count( $post = 0 ) {
$post = get_post( $post );
if ( ! $post ) {
return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
}
if ( ! wp_revisions_enabled( $post ) ) {
return new WP_Error( 'revisions_not_enabled', __( 'Revisions not enabled.' ) );
}
$args = array(
'post_parent' => $post->ID,
'fields' => 'ids',
'post_type' => 'revision',
'post_status' => 'inherit',
'order' => 'DESC',
'orderby' => 'date ID',
'posts_per_page' => 1,
'ignore_sticky_posts' => true,
);
$revision_query = new WP_Query();
$revisions = $revision_query->query( $args );
if ( ! $revisions ) {
return array(
'latest_id' => 0,
'count' => 0,
);
}
return array(
'latest_id' => $revisions[0],
'count' => $revision_query->found_posts,
);
}
Changelog
Version | Description |
---|---|
6.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.