On this page
wp_get_latest_revision_id_and_total_count( int|WP_Post $post ): array|WP_Error
Returns the latest revision ID and count of revisions for a post.
Parameters
Return
array|WP_Error Returns associative array with latest revision ID and total count, or a WP_Error if the post does not exist or revisions are not enabled.
latest_idintThe latest revision post ID or 0 if no revisions exist.countintThe total count of revisions for the given post.
Source
File: wp-includes/revision.php. View all references
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,
);
}
Related
Uses
| Uses | Description |
|---|---|
| WP_Query::__construct() wp-includes/class-wp-query.php | Constructor. |
| wp_revisions_enabled() wp-includes/revision.php | Determines whether revisions are enabled for a given post. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| wp_get_post_revisions_url() wp-includes/revision.php | Returns the url for viewing and potentially restoring revisions of a given post. |
| register_and_do_post_meta_boxes() wp-admin/includes/meta-boxes.php | Registers the default post meta boxes, and runs the |
| wp_update_custom_css_post() wp-includes/theme.php | Updates the |
| WP_REST_Posts_Controller::prepare_links() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares links for the request. |
Changelog
| Version | Description |
|---|---|
| 6.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_latest_revision_id_and_total_count