wp_get_post_autosave()WP 2.6.0

Retrieves the autosaved data of the specified post.

Returns a post object with 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.

No Hooks.

Returns

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

Usage

wp_get_post_autosave( $post_id, $user_id );
$post_id(int) (required)
The post ID.
$user_id(int)
The post author ID.

Changelog

Since 2.6.0 Introduced.

wp_get_post_autosave() code WP 6.8.1

function wp_get_post_autosave( $post_id, $user_id = 0 ) {
	$args = array(
		'post_type'      => 'revision',
		'post_status'    => 'inherit',
		'post_parent'    => $post_id,
		'name'           => $post_id . '-autosave-v1',
		'posts_per_page' => 1,
		'orderby'        => 'date',
		'order'          => 'DESC',
		'fields'         => 'ids',
		'no_found_rows'  => true,
	);

	if ( 0 !== $user_id ) {
		$args['author'] = $user_id;
	}

	$query = new WP_Query( $args );

	if ( ! $query->have_posts() ) {
		return false;
	}

	return get_post( $query->posts[0] );
}