Used a whole day to figure out how to use wordpress REST api to create a custom post with meta info. the trick is you need to register the meta before you can use it in rest api. here’s how to do it:
For example: you have a custom post type: person and the person post type has a meta: name(person’s name). Before you can create, edit the person post type using REST api, you need to add these into your plugin or theme’s functions.php
function my_register_meta(){
$object_type ='post';
$meta_key = 'name'; //the meta's key
$args = array(
'object_subtype' => 'person', //your custom post type's name
'single' => true,
'type' => 'string',
'description' => 'this is the person post type's meta key: name',
'show_in_rest' => true,
'sanitize_callback' => '',
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
);
register_meta($object_type,$meta_key, $args);
}
//run the register_meta function when rest api init
add_action( 'rest_api_init', 'my_register_meta' );
By the way, you also need to enable your custom post type’s show_in_rest, make it to true, otherwise you can not use the custome post type in REST api.