函数文档

wp_save_revisioned_meta_fields()

💡 云策文档标注

概述

wp_save_revisioned_meta_fields() 函数用于保存修订版本的元字段。它通过遍历指定文章类型的可修订元键,将元数据从原始文章复制到修订版本中。

关键要点

  • 函数接受两个必需参数:$revision_id(修订版本的ID)和$post_id(关联文章的ID)。
  • 内部逻辑包括获取文章类型、检查元键是否存在,并使用_wp_copy_post_meta() 复制元数据。
  • 该函数依赖于 wp_post_revision_meta_keys() 来确定哪些元字段应被修订。
  • 自 WordPress 6.4.0 版本引入。

代码示例

function wp_save_revisioned_meta_fields( $revision_id, $post_id ) {
    $post_type = get_post_type( $post_id );
    if ( ! $post_type ) {
        return;
    }

    foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) {
        if ( metadata_exists( 'post', $post_id, $meta_key ) ) {
            _wp_copy_post_meta( $post_id, $revision_id, $meta_key );
        }
    }
}

注意事项

  • 确保 $revision_id 和 $post_id 是有效的整数,否则函数可能提前返回。
  • 元键的修订行为由 wp_post_revision_meta_keys() 控制,开发者可以过滤此函数以自定义可修订的元字段。
  • 复制元数据时使用 _wp_copy_post_meta(),这是一个内部函数,通常不建议直接调用。

📄 原文内容

Save the revisioned meta fields.

Parameters

$revision_idintrequired
The ID of the revision to save the meta to.
$post_idintrequired
The ID of the post the revision is associated with.

Source

function wp_save_revisioned_meta_fields( $revision_id, $post_id ) {
	$post_type = get_post_type( $post_id );
	if ( ! $post_type ) {
		return;
	}

	foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) {
		if ( metadata_exists( 'post', $post_id, $meta_key ) ) {
			_wp_copy_post_meta( $post_id, $revision_id, $meta_key );
		}
	}
}

Changelog

Version Description
6.4.0 Introduced.