I am trying to modify the response object of a WP REST API (v2) call following this guide: http://v2.wp-api.org/extending/modifying/
I have a post type called Careers and a taxonomy called Regions.
Both are set to "show_in_rest" => true
I am using pretty permalinks.
When I GET mydomain/wp-json/wp/v2/careers
it returns results as expected.
So far so good.
I wanted to return the taxonomy terms in the response so I added the following code to functions.php
add_action( 'rest_api_init', 'add_location_to_career_endpoint' );
function add_location_to_career_endpoint() {
register_rest_field( 'career',
'regions',
array(
'get_callback' => 'career_get_the_region',
'update_callback' => null,
'schema' => null,
)
);
}
function career_get_the_region( $object, $field_name, $request ) {
return get_the_terms( $object[ 'id' ], $taxonomies, null);
}
This returns an array of terms against the key “regions” on one instance of WordPress (my development version).
My problem is when I move to staging (identical setup to dev) the taxonomy terms arent being returned.
register_rest_field()
is running because if I add gibberish in $attribute (‘regions’) it throws an error. However the “regions” key and array is not being returned in my results.
Any help here would be much appreciated.