wp_get_post_autosave()
云策文档标注
概述
wp_get_post_autosave() 函数用于检索指定文章的自动保存数据。它返回一个包含自动保存信息的 WP_Post 对象,如果未找到则返回 false。
关键要点
- 函数返回 WP_Post 对象或 false,表示成功检索到自动保存数据或失败/不存在。
- 参数 $post_id 是必需的,指定文章 ID;$user_id 是可选的,用于指定用户 ID,默认为 0 以获取最新自动保存。
- 内部使用 WP_Query 查询 revision 类型的文章,通过 post_parent 和 name 参数匹配自动保存版本。
代码示例
function wp_get_post_autosave( $post_id, $user_id = 0 ) {
$args = array(
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post_id,
'name' => $post_id . '-autosave-v1',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'fields' => 'ids',
'no_found_rows' => true,
);
if ( 0 !== $user_id ) {
$args['author'] = $user_id;
}
$query = new WP_Query( $args );
if ( ! $query->have_posts() ) {
return false;
}
return get_post( $query->posts[0] );
}
原文内容
Retrieves 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.
Parameters
$post_idintrequired-
The post ID.
$user_idintoptional-
The post author ID. Default 0.
Source
function wp_get_post_autosave( $post_id, $user_id = 0 ) {
$args = array(
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post_id,
'name' => $post_id . '-autosave-v1',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'fields' => 'ids',
'no_found_rows' => true,
);
if ( 0 !== $user_id ) {
$args['author'] = $user_id;
}
$query = new WP_Query( $args );
if ( ! $query->have_posts() ) {
return false;
}
return get_post( $query->posts[0] );
}
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |