WP-JSON save custom post type meta fails on update_callback

With wp-json I try to save on post creation post metas as it follows. On update_callback I can see the data what I send but this won’t save

register_rest_field(
‘survey’,
‘survey_data’,
array(
‘get_callback’ => array( $this, ‘getPostMeta’ ),
‘update_callback’ => array($this, ‘updateMeta’),
‘schema’ => null,
)
);

here is the method if I dump $meta I get the json as array, but this will be not saved because I get back false on add_post_meta

public function updateMeta($meta, $object)
{
$hasMetaField = get_post_meta($object->id, ‘survey_data’, false);

if ($hasMetaField) {
$ret = update_post_meta($object->id, ‘survey_data’, $meta );
return true;
} else {

$ret = add_post_meta( $object->id, ‘survey_data’, $meta ,true );
var_dump($ret);
die;
return true;
}
}

Read more here:: WP-JSON save custom post type meta fails on update_callback

Leave a Reply

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