Can’t set custom meta fields for a post

I am trying to add some custom meta variables to a custom post type, but I can’t see the new variables when I dump a posts meta data, and I can’t access it in

Here is the post definition from my functions.php file

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’);

If I create a team post and tried dumping the meta fields, but my new fields don’t appear:

$meta_values = get_post_meta( get_the_ID() );
var_dump( $meta_values );

result:

array(2) {
[“_edit_lock”]=>
array(1) {
[0]=>
string(12) “1566159392:1”
}
[“_edit_last”]=>
array(1) {
[0]=>
string(1) “1”
}
}

I also tried using my custom meta fields in a Gutenberg block.

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”,
}
}

but when I add my block and save the variables don’t update and remain empty.

Am I using register_post_meta correctly?

Read more here:: Can’t set custom meta fields for a post

Leave a Reply

Your email address will not be published. Required fields are marked *