I would like to register custom fields to the Wordpress Rest API. So far I have the code at the bottom of this message, which works for registering one field.
How can I register/nest multiple custom fields under a single key?
I want my JSON to look like this, where everything under the "location" key is a custom field, returning those values from my custom field data:
"location" : {
"mapZoom":
"mapLat":
"mapLng":
"markerLat":
"markerLng":
"addressTitle":
"address":
"city":
"state":
"addressCountry":
"geoJson": [ "mapLng", "mapLat" ]
},
Here's my code for registering single key / value pairs.
add_action('rest_api_init', 'register_custom_fields');
function register_custom_fields(){
register_rest_field(
'post',
'formatted',
array(
'get_callback' => 'show_fields'
)
);
}
function show_fields($object, $field_name, $request){
return get_post_meta($object['id'], $field_name, true);
}