wp_get_post_revisions()WP 2.6.0

Gets all revisions (edits, changes) of the specified post.

Read more about revisions in the description of the function wp_revisions_to_keep().

The base inclusion of revisions is controlled by the constant WP_POST_REVISIONS, which needs to be defined in the WP plugin or earlier.

if( ! defined('WP_POST_REVISIONS') ) define( 'WP_POST_REVISIONS', 5 );

To ensure that revisions are supported by new post types, support for revisions must be specified when registering them: the 'supports' parameter in register_post_type()

post_type_supports( $post->post_type, 'revisions' );

If you need to add support for revisions after the post type has been registered, use add_post_type_support().

You can also change the number of revisions through the hook wp_revisions_to_keep. However, it will not work if the post type does not support revisions...

No Hooks.

Returns

WP_Post[]|Int[]. An array of revision post objects or array() if there are no revisions.

Usage

wp_get_post_revisions( $post_id, $args );
$post_id(int/WP_Post)
ID of the post whose revisions need to be retrieved. You can pass a post object.
Default: global $post
$args(string/array)
Additional parameters. All parameters accepted by get_children(). This parameter is most often not needed.
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 7.0

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;
}