WordPress at a glance

wp_get_post_revision() WP 2.6.0

Gets a post revision by revision's ID. If there is no such revision, null will be returned.

The revisions are posts with post_type = revision.

Works based on: get_post()
✈ 1 time = 0.000602s = slow | 50000 times = 1.12s = fast

No Hooks.

Return

WP_Post/Array/null.

  • WP_Post on success if $output = OBJECT.
  • Array on success if $output = ARRAY_A or ARRAY_N.
  • null on failure (if the revision doesn't exist).

Usage

wp_get_post_revision( $post, $output, $filter );
$post(int/WP_Post) (required) (passed by reference — &)
The post ID or object.
$output(string)
How to return data. Can be: OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively.
Default: OBJECT
$filter(string)
Optional sanitation filter. See sanitize_post_field().
Default: 'raw'

Examples

#1 Get WP_Post object which represents a revision

$revision_id = 15;
$revision    = wp_get_post_revision( $revision_id );

if( $revision ){
	// yes it's a revision
	print_r( $revision );
}

/* Output
WP_Post Object
(
	[ID] => 1008
	[post_author] => 1
	[post_date] => 2015-08-26 19:15:42
	[post_date_gmt] => 2015-08-26 15:15:42
	[post_content] => Content
	[post_title] => The main ways to learn
	[post_excerpt] => 
	[post_status] => inherit
	[comment_status] => closed
	[ping_status] => closed
	[post_password] => 
	[post_name] => 997-revision-v1
	[to_ping] => 
	[pinged] => 
	[post_modified] => 2015-08-26 19:15:42
	[post_modified_gmt] => 2015-08-26 15:15:42
	[post_content_filtered] => 
	[post_parent] => 997
	[guid] => http://example.com/997-revision-v1/
	[menu_order] => 0
	[post_type] => revision
	[post_mime_type] => 
	[comment_count] => 0
	[filter] => raw
)
*/

#2 Check if the post is a revision

This is the code of wp_is_post_revision() function which demonstrates how to check if the post is a revision:

function wp_is_post_revision( $post ) {

	if ( ! $post = wp_get_post_revision( $post ) ) {
		return false;
	}

	return (int) $post->post_parent;
}

Code of wp get post revision: wp-includes/revision.php WP 5.2.1

<?php
function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
	if ( ! $revision = get_post( $post, OBJECT, $filter ) ) {
		return $revision;
	}
	if ( 'revision' !== $revision->post_type ) {
		return null;
	}

	if ( $output == OBJECT ) {
		return $revision;
	} elseif ( $output == ARRAY_A ) {
		$_revision = get_object_vars( $revision );
		return $_revision;
	} elseif ( $output == ARRAY_N ) {
		$_revision = array_values( get_object_vars( $revision ) );
		return $_revision;
	}

	return $revision;
}

Related Functions

From tag: post revisions

No comments
    Hello, !     Log In . Register