函数文档

wp_is_post_autosave()

💡 云策文档标注

概述

wp_is_post_autosave() 函数用于判断指定文章是否为自动保存版本。它接受文章 ID 或 WP_Post 对象作为参数,返回自动保存的父文章 ID 或 false。

关键要点

  • 参数 $post 为必需,可以是整数(文章 ID)或 WP_Post 对象。
  • 返回值:成功时返回自动保存的父文章 ID(整数),如果不是修订版本则返回 false。
  • 内部通过 wp_get_post_revision() 获取修订版本,并检查 post_name 是否包含父文章 ID 和 "-autosave" 后缀。

代码示例

// 如果文章是自动保存,则返回。
if ( wp_is_post_autosave( $post_id ) ) {
    return;
}

📄 原文内容

Determines if the specified post is an autosave.

Parameters

$postint|WP_Postrequired
Post ID or post object.

Return

int|false ID of autosave’s parent on success, false if not a revision.

Source

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

Changelog

Version Description
2.6.0 Introduced.

User Contributed Notes