3

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.

  • Do you have taxonomy terms available on the staging? Or the object have the terms assigned to it? If you are using different db tables for staging. Then double check them.. – Sisir Feb 15 '16 at 14:49
1

The issue was my staging and dev were using one minor version difference of the WP API plugin which Im assuming made a destructive change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.