wp_is_post_autosave()WP 2.6.0

Determines whether the specified post is an autosave.

When editing, the post is periodically saved, such a save is recorded in a separate row of the wp_posts table with the post type {ID-parent post}-autosave-v1. This function checks whether the specified post is such an autosave.

1 time — 0.001271 sec (very slow) | 50000 times — 0.22 sec (very fast) | PHP 7.1.5, WP 4.8.2

No Hooks.

Returns

Int|false.

  • False if not an autosave (not a post revision).
  • ID of the main post, of which the specified post is an autosave.

Usage

wp_is_post_autosave( $post );
$post(integer/wp_post) (required)
ID or object of the post (record).

Examples

0

#1 Check if the specified post is an autosave (revision)

$post_id = 20;
if ( wp_is_post_autosave( $post_id ) ) {
	return;
}

Changelog

Since 2.6.0 Introduced.

wp_is_post_autosave() code WP 7.0

function wp_is_post_autosave( $post ) {
	$post = wp_get_post_revision( $post );

	if ( ! $post ) {
		return false;
	}

	if ( str_contains( $post->post_name, "{$post->post_parent}-autosave" ) ) {
		return (int) $post->post_parent;
	}

	return false;
}