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.
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. |
Skip to note 2 content
Codex
Check if a saved post is an autosave post, like saving meta box data.
// If post is an autosave, return. if ( wp_is_post_autosave( $post_id ) ) { return; }