0

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?

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

3 Answers 3

1

When registering the Custom Post Type, ensure custom-fields is present in supports => array('editor','title', 'custom-fields'). It's the legacy wording of meta fields and is commonly mistakenly omitted when declaring supports in CPTs.

After you make the above change, your Team CPT should work with meta values. You still may encounter in Gutenberg initially, the meta values are 'undefined' as they don't have a default value set. A default value can be set either when registering the Post Meta eg. 'default' => 'test string' or in the block attributes.

NB. The documentation has been updated since this question was posted ref: trac #47866 Refer to the latest WordPress REST API to see all the changes

0

For anyone else reading this old thread:

Here is how it should've been done


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

-1

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

add_action('init','345376_register_metaof_team');
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 );

}
3
  • 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
    Commented Aug 19, 2019 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
    Commented Sep 8, 2019 at 15:09
  • @SkyShab okay, updated my answer.
    – Den Pat
    Commented Sep 8, 2019 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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