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.

1
  • 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
    Commented Feb 15, 2016 at 14:49

1 Answer 1

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 and acknowledge you have read our privacy policy.

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