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.

Хуков нет.

Возвращает

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

Использование

wp_get_post_autosave( $post_id, $user_id );
$post_id(int) (обязательный)
The post ID.
$user_id(int)
The post author ID.

Заметки

  • Global. wpdb. $wpdb WordPress database abstraction object.

Список изменений

С версии 2.6.0 Введена.

Код wp_get_post_autosave() WP 6.4.3

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] );
}