Skip Navigation

[Resolved] Custom field taxonomy register in api

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to add the latitude and longitude values of a custom taxonomy address field in my taxonomy REST API response.

Solution: Use the types_render_termmeta function to render an address field with PHP.

Relevant Documentation: https://toolset.com/documentation/customizing-sites-using-php/functions/#address

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by avansisI-2 4 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1866529

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?

#1867071

It sounds like the direccion_zonas custom field is applied to a taxonomy, not a custom post type. Is that correct? If you want to render a taxonomy field value, you must use the types_render_termmeta function instead of the types_render_field function. The types_render_field function only works with custom post fields, not custom taxonomy fields.

You must pass in the term ID using the attribute term_id. The post_id attribute doesn't do anything in types_render_termmeta:
https://toolset.com/documentation/customizing-sites-using-php/functions/#address

'index' =>1,

The index attribute should only be used if the address field is a repeater field. If the address field does not repeat, you should omit this index attribute.

#1867455

My issue is resolved now. Thank you!