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', [
            'add_new_item'=>'Add New Team Member',
            'edit_item'=>'Edit Team Member',
            'all_items'=>'All Team Member'
        'show_in_rest' => true,
        // 'supports' => array('editor','title', 'custom-fields'),
        'supports' => array('editor','title'),
        'rewrite' => array('slug' => 'teams'),
        'template' => array(
            array( 'dqcblocks/member' )

    $fields = [

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


array(2) {
  array(1) {
    string(12) "1566159392:1"
  array(1) {
    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?

  • The registration seems correct to me. Could it be that there is no sanitize_callback and other required fields in register_post_meta? – Alvaro Aug 19 '19 at 15:50

Try Fire it on init instead of inside register_post_type like this:

function 345376_register_metaof_team(){

$object_type = 'post';
$args = array(
    'type'         => 'string',
    'description'  => 'A meta key associated with post views.',
    'single'       => true,
    'show_in_rest' => true,

register_meta( $object_type, 'post_views', $args );

  • Thanks for the answer, but I don' think this is the issue as I am not firing it inside of register_post_type. but in a function that is run on init at the same time as register_post_type. I tried separating it into its won function anyway, but as I suspected, it didn't make a difference. – Finglish Aug 19 '19 at 6:23
  • 2
    FYI, when using register_meta, you don't pass the post type for the first parameter. This would be "post" no matter what post type you are using. – SkyShab Sep 8 '19 at 15:09
  • @SkyShab okay, updated my answer. – Den Pat Sep 8 '19 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.