我正在尝试向自定义帖子类型添加一些自定义元变量,但在转储帖子元数据时,我看不到新的变量,并且无法在中访问它
这是我的函数中的帖子定义。php文件
function register_team_post(){
register_post_type(\'team\', [
\'public\'=>true,
\'labels\'=>array(
\'name\'=>\'Team\',
\'add_new_item\'=>\'Add New Team Member\',
\'edit_item\'=>\'Edit Team Member\',
\'all_items\'=>\'All Team Member\'
),
\'menu_icon\'=>\'dashicons-businessman\',
\'show_in_rest\' => true,
// \'supports\' => array(\'editor\',\'title\', \'custom-fields\'),
\'supports\' => array(\'editor\',\'title\'),
\'rewrite\' => array(\'slug\' => \'teams\'),
\'template\' => array(
array( \'dqcblocks/member\' )
)
]);
$fields = [
\'team_member_portrait\',
\'team_member_name\',
\'team_member_title\',
\'team_member_description\'
];
$args = array(\'show_in_rest\' => true, \'type\' => \'string\');
foreach($fields as $field){
register_post_meta( \'team\', $field, $args);
}
}
add_action(\'init\', \'register_team_post\');
如果我创建了一个团队帖子并尝试转储元字段,但我的新字段没有出现:
$meta_values = get_post_meta( get_the_ID() );
var_dump( $meta_values );
结果:
array(2) {
["_edit_lock"]=>
array(1) {
[0]=>
string(12) "1566159392:1"
}
["_edit_last"]=>
array(1) {
[0]=>
string(1) "1"
}
}
我还尝试在古腾堡块中使用我的自定义元字段。
attributes: {
portrait: {
type: "string",
source: "meta",
meta: "team_member_portrait",
},
name: {
type: "string",
source: "meta",
meta: "team_member_name",
},
title: {
type: "string",
source : "meta",
meta: "team_member_title",
},
description: {
type: "string",
source: "meta",
meta: "team_member_description",
}
}
但当我添加块并保存变量时,变量不会更新并保持为空。
我是否正确使用register\\u post\\u meta?
SO网友:Sunny Johal
对于阅读此旧线程的任何其他人:
这是应该怎么做的
function register_team_post() {
register_post_type(
\'team\',
[
\'public\' => true,
\'labels\' => [
\'name\' => \'Team\',
\'add_new_item\' => \'Add New Team Member\',
\'edit_item\' => \'Edit Team Member\',
\'all_items\' => \'All Team Member\',
],
\'menu_icon\' => \'dashicons-businessman\',
\'show_in_rest\' => true,
\'supports\' => [ \'editor\', \'title\', \'custom-fields\' ],
\'rewrite\' => [ \'slug\' => \'teams\' ],
\'template\' => [ [ \'dqcblocks/member\' ] ],
]
);
$fields = [
\'team_member_portrait\',
\'team_member_name\',
\'team_member_title\',
\'team_member_description\',
];
$args = [
\'object_subtype\' => \'team\', // this is the correct place to pass the post type.
\'show_in_rest\' => true,
\'type\' => \'string\',
];
foreach ( $fields as $field ) {
register_meta( \'post\', $field, $args );
}
}
add_action( \'init\', \'register_team_post\' );