wp_get_post_autosave() WordPress Function

The wp_get_post_autosave() function retrieves the autosave data for the specified post. Autosave data is a copy of the post data that is saved automatically whenever a post is updated. This function can be used to retrieve the autosave data for a post, or to check if a post has been autosaved.

wp_get_post_autosave( int $post_id, int $user_id ) #

Retrieve the autosaved data of the specified post.


Description

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.


Top ↑

Parameters

$post_id

(int)(Required)The post ID.

$user_id

(int)(Optional)The post author ID.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/revision.php

function wp_get_post_autosave( $post_id, $user_id = 0 ) {
	global $wpdb;

	$autosave_name = $post_id . '-autosave-v1';
	$user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null;

	// Construct the autosave query.
	$autosave_query = "
		SELECT *
		FROM $wpdb->posts
		WHERE post_parent = %d
		AND post_type = 'revision'
		AND post_status = 'inherit'
		AND post_name   = %s " . $user_id_query . '
		ORDER BY post_date DESC
		LIMIT 1';

	$autosave = $wpdb->get_results(
		$wpdb->prepare(
			$autosave_query,
			$post_id,
			$autosave_name
		)
	);

	if ( ! $autosave ) {
		return false;
	}

	return get_post( $autosave[0] );
}


Top ↑

Changelog

Changelog
VersionDescription
2.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.