Good afternoon, I need to record the longitude and latitude from custom taxonomy in my API. My code is this, but I can't get it to work.
//LATITUD Y LONGITUD ZONAS COMERCIALES
//
add_action( 'rest_api_init', 'create_api_address_zonas' );
function create_api_address_zonas() {
$taxonomy = 'zonas'; //replace sample-location-a with your post type slug
$field_slug = 'direccion_zonas'; //replace direccion with your custom address field slug
register_rest_field( $taxonomy, $field_slug . '-latitude', array(
'get_callback' => 'get_address_latitude_zonas',
'schema' => null,
)
);
register_rest_field( $taxonomy, $field_slug . '-longitude', array(
'get_callback' => 'get_address_longitude_zonas',
'schema' => null,
)
);
}
function get_address_latitude_zonas( $object ) {
$field_slug = 'direccion_zona_comercial'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field($field_slug, array(
'post_id'=> $post_id,
'format' => 'FIELD_LATITUDE',
'index' =>1,
));
return $res;
}
function get_address_longitude_zonas( $object ) {
$field_slug = 'direccion_zona_comercial'; //replace direccion with your custom address field slug
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
$res = types_render_field($field_slug, array(
'post_id'=> $post_id,
'format' => 'FIELD_LONGITUDE',
'index' =>1,
));
return $res;
}
The json it returns is this.
{
"id": 26,
"count": 12,
"description": "",
"link": "<em><u>hidden link</u></em>",
"name": "Centro Comercial Burgo Centro 1",
"slug": "centro-comercial-burgo-centro-1",
"taxonomy": "zonas",
"parent": 0,
"meta": [
],
"direccion_zonas-latitude": "",
"direccion_zonas-longitude": "",
"toolset-meta": {
"direccion-zonas": {
"direccion_zona_comercial": {
"type": "google_address",
"raw": "Calle Comunidad de Madrid, 37-41, 28231 Las Rozas de Madrid, España"
}
Could you help me?