wp_get_post_autosave

Retrieve the autosaved data of the specified post.

Description

(WP_Post|false) wp_get_post_autosave( (int) $post_id, (int) $user_id = 0 ); 

Returns a post object containing the information that was autosaved for the specified post. If the optional $user_id is passed, returns the autosave for that user otherwise returns the latest autosave.

Returns (WP_Post|false)

The autosaved data or false on failure or when no autosave exists.

Parameters (2)

0. $post_id (int)
The post id.
1. $user_id — Optional. (int)
The post author ID.

Usage

  1. if ( !function_exists( 'wp_get_post_autosave' ) ) { 
  2. require_once ABSPATH . WPINC . '/revision.php'; 
  3.  
  4. // The post id. 
  5. $post_id = -1; 
  6.  
  7. // Optional The post author ID. 
  8. $user_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_get_post_autosave($post_id, $user_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/revision.php  
  1. function wp_get_post_autosave( $post_id, $user_id = 0 ) { 
  2. $revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) ); 
  3.  
  4. foreach ( $revisions as $revision ) { 
  5. if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) { 
  6. if ( $user_id && $user_id != $revision->post_author ) 
  7. continue; 
  8.  
  9. return $revision; 
  10.  
  11. return false;