函数文档

_wp_post_revision_fields()

💡 云策文档标注

概述

_wp_post_revision_fields() 函数用于确定在文章修订中保存哪些字段。它返回一个可版本化的字段数组,并允许通过过滤器进行自定义。

关键要点

  • 函数返回一个字符串数组,包含默认可版本化的字段:'post_title'、'post_content' 和 'post_excerpt'。
  • 通过 apply_filters('_wp_post_revision_fields', $fields, $post) 钩子,开发者可以过滤修订字段列表。
  • 某些字段(如 'ID'、'post_name' 等)被内部保护,不能版本化,函数会自动移除它们。
  • 参数 $post 可选,可以是文章数组或 WP_Post 对象,用于处理修订插入;$deprecated 参数已弃用。

代码示例

// 获取修订字段列表
foreach ( _wp_post_revision_fields() as $field => $field_title ) : /* ... */

// 准备文章作为修订插入数据库
$post = _wp_post_revision_fields( $post );

// 用于自动保存(使用已弃用参数)
$post = _wp_post_revision_fields( $post, true );

注意事项

  • 从版本 4.5.0 开始,$autosave 参数被重命名为 $deprecated,不再使用。
  • 函数内部使用静态变量缓存字段列表,以提高性能。
  • 相关函数包括 _wp_post_revision_data()、wp_save_post_revision() 等,用于处理修订逻辑。

📄 原文内容

Determines which fields of posts are to be saved in revisions.

Parameters

$postarray|WP_Postoptional
A post array or a WP_Post object being processed for insertion as a post revision.

Default:array()

$deprecatedbooloptional
Not used.

Default:false

Return

string[] Array of fields that can be versioned.

Source

function _wp_post_revision_fields( $post = array(), $deprecated = false ) {
	static $fields = null;

	if ( ! is_array( $post ) ) {
		$post = get_post( $post, ARRAY_A );
	}

	if ( is_null( $fields ) ) {
		// Allow these to be versioned.
		$fields = array(
			'post_title'   => __( 'Title' ),
			'post_content' => __( 'Content' ),
			'post_excerpt' => __( 'Excerpt' ),
		);
	}

	/**
	 * Filters the list of fields saved in post revisions.
	 *
	 * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
	 *
	 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
	 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
	 * and 'post_author'.
	 *
	 * @since 2.6.0
	 * @since 4.5.0 The `$post` parameter was added.
	 *
	 * @param string[] $fields List of fields to revision. Contains 'post_title',
	 *                         'post_content', and 'post_excerpt' by default.
	 * @param array    $post   A post array being processed for insertion as a post revision.
	 */
	$fields = apply_filters( '_wp_post_revision_fields', $fields, $post );

	// WP uses these internally either in versioning or elsewhere - they cannot be versioned.
	foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) {
		unset( $fields[ $protect ] );
	}

	return $fields;
}

Hooks

apply_filters( ‘_wp_post_revision_fields’, string[] $fields, array $post )

Filters the list of fields saved in post revisions.

Changelog

Version Description
4.5.0 The optional $autosave parameter was deprecated and renamed to $deprecated.
2.6.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Using the function to get the list of revision fields:

    foreach ( _wp_post_revision_fields() as $field => $field_title ) : /* ... */

    Using the function to prepare a post for insertion into the database as a revision:

    $post = _wp_post_revision_fields( $post );

    For an autosave:

    $post = _wp_post_revision_fields( $post, true );