wp_get_post_revision()
云策文档标注
概述
wp_get_post_revision() 函数用于获取文章修订版本。它接受文章 ID 或对象作为参数,并支持多种返回类型。
关键要点
- 函数用于获取文章修订版本,基于 get_post() 实现。
- 参数 $post 为必需,可以是整数 ID 或 WP_Post 对象。
- 参数 $output 可选,指定返回类型:OBJECT(默认,返回 WP_Post 对象)、ARRAY_A(关联数组)或 ARRAY_N(数字数组)。
- 参数 $filter 可选,指定 sanitization 过滤器,默认 'raw'。
- 返回 WP_Post 对象、数组或 null(失败时)。
- 函数内部检查 post_type 是否为 'revision',否则返回 null。
代码示例
function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
$revision = get_post( $post, OBJECT, $filter );
if ( ! $revision ) {
return $revision;
}
if ( 'revision' !== $revision->post_type ) {
return null;
}
if ( OBJECT === $output ) {
return $revision;
} elseif ( ARRAY_A === $output ) {
$_revision = get_object_vars( $revision );
return $_revision;
} elseif ( ARRAY_N === $output ) {
$_revision = array_values( get_object_vars( $revision ) );
return $_revision;
}
return $revision;
}注意事项
- 确保传入的 $post 参数有效,否则可能返回 null。
- 使用 $output 参数灵活控制返回数据类型,便于不同场景处理。
- 函数自 WordPress 2.6.0 版本引入,相关函数包括 wp_is_post_revision()、wp_restore_post_revision() 等。
原文内容
Gets a post revision.
Parameters
$postint|WP_Postrequired-
Post ID or post object.
$outputstringoptional-
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively.
Default:
OBJECT $filterstringoptional-
Optional sanitization filter. See sanitize_post() . Default
'raw'.
Source
function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
$revision = get_post( $post, OBJECT, $filter );
if ( ! $revision ) {
return $revision;
}
if ( 'revision' !== $revision->post_type ) {
return null;
}
if ( OBJECT === $output ) {
return $revision;
} elseif ( ARRAY_A === $output ) {
$_revision = get_object_vars( $revision );
return $_revision;
} elseif ( ARRAY_N === $output ) {
$_revision = array_values( get_object_vars( $revision ) );
return $_revision;
}
return $revision;
}
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |