I am assuming you want these taxonomies data in each item of post_type JSON, but the logic is the same for taxonomies endpoint and JSON response.
I see two ways of achieving it.
- filtering the data using
rest_prepare_post_type
filter (Reference) or
- creating new field(s) using the
register_rest_field
function. (Reference)
I would recommend the second approach. This is the one I've used recently with a custom post type
Case 1: rest_prepare_post_type
In the first case, you would use the rest_prepare_post_type
filter to get the data, generate the data you want, add it to the data array and return the filtered data. In this case, if the post type is "post", the hook is rest_prepare_post.
/**
* @summary Filters post rest response
*
* @description Filters post rest response removing or adding certain information
* @param object $data The response object.
* @param object $post The original post type object.
* @param object $request Request used to generate the response.
* @return object $data The filtered response object.
*/
function wpse_283452_rest_prepare_post($data, $post, $request) {
$_data = $data->data;
// say you want to remove author from the response
unset($_data['author']);
// if you want to ADD fields, you would do the logic here and...
// add it to the $_data array
$data->data = $_data;
// and finally return it
return $data;
}
add_filter('rest_prepare_post', 'wpse_283452_rest_prepare_post', 10, 3);
If you want to the same for taxonomies, you would use rest_prepare_taxonomy/
(reference)
Case 2: register_rest_field (recommended)
You will register a new field with register_rest_field
function and populate it the way you want using a callback
/**
* Registers a new field on an existing WordPress object type.
*
* @since 4.7.0
*
* @global array $wp_rest_additional_fields Holds registered fields, organized
* by object type.
*
* @param string|array $object_type Object(s) the field is being registered
* to, "post"|"term"|"comment" etc.
* @param string $attribute The attribute name.
* @param array $args {
* Optional. An array of arguments used to handle the registered field.
*
* @type string|array|null $get_callback Optional. The callback function used to retrieve the field
* value. Default is 'null', the field will not be returned in
* the response.
* @type string|array|null $update_callback Optional. The callback function used to set and update the
* field value. Default is 'null', the value cannot be set or
* updated.
* @type string|array|null $schema Optional. The callback function used to create the schema for
* this field. Default is 'null', no schema entry will be returned.
* }
*/
register_rest_field(
'post',
'wpse_283452_taxonomies',
array(
'get_callback' => function ($object, $field_name, $request) {
return wpse_283452_taxonomies_callback($object);
},
)
);
/**
* @summary Return and array with taxonomy info
*
* @description Returns an array with taxonomy information in a custom way
* @param object $object The rest item original object
* @return array $custom_taxonom an array with taxonomy terms with their information in a custom way
*/
function wpse_283452_taxonomies_callback($object) {
$post_id = $object['id'];
$terms = wp_get_post_terms(
$post_id,
'business_type',
array(
'fields' => 'all',
)
);
// here you should construct an array with the information in $terms in the way/structure you want
return $terms;
}
For taxonomies, you would use the taxonomy slug instead of 'post' as the 1st argument on register_rest_field.
That's it.
Let me know if works.