wp_restore_post_revision_meta()
云策文档标注
概述
wp_restore_post_revision_meta() 函数用于恢复文章的修订版元数据到当前文章。它通过遍历可修订的元字段,先清除现有元数据,再从指定修订版复制元数据。
关键要点
- 函数接受两个必需参数:$post_id(目标文章ID)和$revision_id(源修订版ID)。
- 内部逻辑包括:获取文章类型,遍历 wp_post_revision_meta_keys() 返回的可修订元字段,使用 delete_post_meta() 清除现有元数据,并通过 _wp_copy_post_meta() 从修订版复制元数据。
- 相关函数包括 wp_post_revision_meta_keys()、_wp_copy_post_meta()、delete_post_meta() 和 get_post_type()。
代码示例
function wp_restore_post_revision_meta( $post_id, $revision_id ) {
$post_type = get_post_type( $post_id );
if ( ! $post_type ) {
return;
}
// Restore revisioned meta fields.
foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) {
// Clear any existing meta.
delete_post_meta( $post_id, $meta_key );
_wp_copy_post_meta( $revision_id, $post_id, $meta_key );
}
}注意事项
- 该函数从 WordPress 6.4.0 版本开始引入。
- 如果 $post_id 对应的文章类型无效,函数会提前返回而不执行任何操作。
- 依赖于 wp_post_revision_meta_keys() 来确定哪些元字段是可修订的,确保元数据恢复的准确性。
原文内容
Restore the revisioned meta values for a post.
Parameters
$post_idintrequired-
The ID of the post to restore the meta to.
$revision_idintrequired-
The ID of the revision to restore the meta from.
Source
function wp_restore_post_revision_meta( $post_id, $revision_id ) {
$post_type = get_post_type( $post_id );
if ( ! $post_type ) {
return;
}
// Restore revisioned meta fields.
foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) {
// Clear any existing meta.
delete_post_meta( $post_id, $meta_key );
_wp_copy_post_meta( $revision_id, $post_id, $meta_key );
}
}
Changelog
| Version | Description |
|---|---|
| 6.4.0 | Introduced. |