wp_get_post_revision()
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.
Uses: get_post()
Used By: wp_is_post_autosave(), wp_is_post_revision()
1 time — 0.000602 sec (slow) | 50000 times — 1.12 sec (fast)
No Hooks.
Returns
WP_Post|Array|null.
WP_Poston success if $output = OBJECT.Arrayon success if $output = ARRAY_A or ARRAY_N.nullon 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;
}
Changelog
| Since 2.6.0 | Introduced. |