register_post_meta()
云策文档标注
概述
register_post_meta() 函数用于为文章类型注册元数据键,是 WordPress 元数据注册系统的一部分。它封装了 register_meta() 函数,专门处理文章相关的元数据注册。
关键要点
- 函数用于注册文章类型的元数据键,支持指定特定文章类型或所有现有文章类型。
- 参数包括 $post_type(文章类型,空字符串表示所有类型)、$meta_key(元数据键)和 $args(配置数组)。
- 返回布尔值,表示注册是否成功。
- 内部调用 register_meta() 函数,并设置 object_subtype 参数。
- 自定义文章类型需要支持 custom-fields 才能使用此函数。
注意事项
- 确保自定义文章类型在 register_post_type() 的 supports 数组中包含 'custom-fields'。
- 此函数在 WordPress 核心中仅由 register_post_meta 和 register_term_meta 使用,Jetpack 等插件也有应用。
原文内容
Registers a meta key for posts.
Parameters
$post_typestringrequired-
Post type to register a meta key for. Pass an empty string to register the meta key across all existing post types.
$meta_keystringrequired-
The meta key to register.
$argsarrayrequired-
Data used to describe the meta key when registered. See register_meta() for a list of supported arguments.
Source
function register_post_meta( $post_type, $meta_key, array $args ) {
$args['object_subtype'] = $post_type;
return register_meta( 'post', $meta_key, $args );
}
Changelog
| Version | Description |
|---|---|
| 4.9.8 | Introduced. |
Skip to note 3 content
shramee
If you are wondering why it doesn’t work for your custom post type when doing a block editor plugin or something,
Your custom post type needs to support
custom-fields.register_post_type( 'book', array( ... 'supports' => array( 'title', 'editor', 'custom-fields', ..., ), // Make sure you add custom-fields here ^^^^^^^^^^^^^ ... ) );Skip to note 4 content
kitchin
In WP Core, `register_meta() ` is only used by `register_post_meta` and `register_term_meta() `, and otherwise none of these functions are used in Core.
Jetpack uses `register_post_meta() `, and `register_meta() ` for ‘post’ and ‘user’.