wp_get_post_revisions()WP 2.6.0

Returns all revisions of specified post.

No Hooks.

Return

WP_Post[]|Int[]. Array of revision objects or IDs, or an empty array if none.

Usage

wp_get_post_revisions( $post, $args );
$post(int|WP_Post)
Post ID or WP_Post object.
Default: global $post
$args(array|null)
Arguments for retrieving post revisions.
Default: null

Examples

0

#1 Get the latest revision of the post

$post_id = 999;
$revisions = wp_get_post_revisions( $post_id );

$last_revision = false;

// Get the latest version but not the autosave
foreach ( $revisions as $revision ) {

	if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
		$last_revision = $revision;
		break;
	}
}
0

#2 An example of what the function outputs

$revisions = wp_get_post_revisions( 128 );

/* revision posts 128
Array (
	[22] => WP_Post Object  (
			[ID]                    => 22
			[post_author]           => 0
			[post_date]             => 2017-02-21 01:41:45
			[post_date_gmt]         => 2017-02-20 22:41:45
			[post_content]          => Content of revision ...
			[post_title]            => Ice Cream Recipe
			[post_excerpt]          =>
			[post_status]           => inherit
			[comment_status]        => closed
			[ping_status]           => closed
			[post_password]         =>
			[post_name]             => 128-revision-v1
			[to_ping]               =>
			[pinged]                =>
			[post_modified]         => 2017-02-21 01:41:45
			[post_modified_gmt]     => 2017-02-20 22:41:45
			[post_content_filtered] =>
			[post_parent]           => 128
			[guid]                  => http://example.com/recept-morojenogo/128-revision-v1.html
			[menu_order]            => 0
			[post_type]             => revision
			[post_mime_type]        =>
			[comment_count]         => 0
			[filter]                => raw
		)

	[19] => WP_Post Object (
			[ID]                    => 19
			[post_author]           => 0
			[post_date]             => 2017-02-19 15:40:48
			[post_date_gmt]         => 2017-02-19 12:40:48
			[post_content]          => Revision content ...
			[post_title]            => Ice Cream Recipe
			[post_excerpt]          =>
			[post_status]           => inherit
			[comment_status]        => closed
			[ping_status]           => closed
			[post_password]         =>
			[post_name]             => 128-revision-v1
			[to_ping]               =>
			[pinged]                =>
			[post_modified]         => 2017-02-19 15:40:48
			[post_modified_gmt]     => 2017-02-19 12:40:48
			[post_content_filtered] =>
			[post_parent]           => 128
			[guid]                  => http://example.com/recept-morojenogo/128-revision-v1.html
			[menu_order]            => 0
			[post_type]             => revision
			[post_mime_type]        =>
			[comment_count]         => 0
			[filter]                => raw
		)

	[12] => WP_Post Object (
			...
		)

)
*/

Notes

Changelog

Since 2.6.0 Introduced.

wp_get_post_revisions() code WP 6.4.3

function wp_get_post_revisions( $post = 0, $args = null ) {
	$post = get_post( $post );

	if ( ! $post || empty( $post->ID ) ) {
		return array();
	}

	$defaults = array(
		'order'         => 'DESC',
		'orderby'       => 'date ID',
		'check_enabled' => true,
	);
	$args     = wp_parse_args( $args, $defaults );

	if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) {
		return array();
	}

	$args = array_merge(
		$args,
		array(
			'post_parent' => $post->ID,
			'post_type'   => 'revision',
			'post_status' => 'inherit',
		)
	);

	$revisions = get_children( $args );

	if ( ! $revisions ) {
		return array();
	}

	return $revisions;
}