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?

  • 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
0

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

}
  • 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.