2

I am able to get the custom REST API fields using meta key values to be formatted like this:

"location": {
    "lat": "000001",
    "long": "000002"
},

The works fine when I am trying to retrieve values of the post but when I am trying to add posts using REST API, the location fields doesn't seem to store anything, when I check my current code gives out an error.

Here is my update function:

    register_rest_field( $this->post_types, 'location', 
        array(
            'get_callback' => array( $this, 'get_location_meta' ), 
            'update_callback' => array( $this, 'update_location_meta' ), 
            'schema' => null
        )
    );

    public function get_location_meta($object, $field_name, $request) {
        return array(
        'lat'    => get_post_meta($object['id'], 'latitude', true),
        'long'   => get_post_meta($object['id'], 'longitude', true),
    );
}

    public function update_location_post_meta($value, $object, $field_name)
        return array(
            'lat'    => update_post_meta($object->ID, 'latitude', true),
            'long'   => update_post_meta($object->ID, 'longitude', true),
        );
    }

The custom field key names in Wordpress are latitude and longitude.

But now I am not sure what will be the field name I will target to be able to fill in some values for the latitude and longitude metakeys. When I try to send a value to location using Postman, I am getting an unexpected error.

Thanks in advance for any suggestion.

Your Answer

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

Browse other questions tagged or ask your own question.