Skip Navigation

[Resolved] I am trying to extract latitude and longitude in PHP

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

Problem:

Extract latitude and longitude from custom address field using PHP codes.

Solution:

I suggest you try Types function types_render_field(), for example:

https://toolset.com/forums/topic/i-am-trying-to-extract-latitude-and-longitude-in-php/#post-1092816

Relevant Documentation:

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

This support ticket is created 6 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 6 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1092501

I am trying to extract latitude and longitude and also name and slug from my terms.

I am using these fields in a JSON. This is my code which works fine:

function places_query_posts_callback() {
  $args = array (
    'post_type' => 'erinnerungsort',
    'orderby' => 'post_title',
    'order' => 'ASC',
  );
  $query = new WP_Query( $args );
  $response = array();
  foreach ( $query->posts as $result ) {
    $geodata_raw = get_post_meta($result->ID, 'wpcf-geodaten', true);
    $geodata_raw = substr($geodata_raw,1,(strlen($geodata_raw)-2));
    $geodata = explode(',', $geodata_raw);
    $data = array();
    $data['id'] = $result->ID;
    $data['lat'] = $geodata[0];
    $data['lng'] = $geodata[1];
    $data['title'] = $result->post_title;
    $data['tooltip'] = get_post_meta($result->ID, 'wpcf-tooltip');
    $data['content'] = $result->post_content;
    $data['teaser'] = substr($result->post_content, 0, 200);
    $data['type'] = array_shift(get_the_terms($result->ID, 'artdesortes'));
    $data['region'] = array_shift(get_the_terms($result->ID, 'region'));
    $data['thumb'] = get_the_post_thumbnail_url($result->ID);
    //$data['images'] = get_post_meta($result->ID, 'wpcf-bild');
    $data['chronik'] = 'chronik';
    $data['link'] = get_permalink($result->ID) ;
    array_push($response, $data);
  }
  exit( json_encode( $response ) );
}

I expected to see:
How do I access latitude & longitude?
How do I access slug, name etc. from my terms ('region' & 'artdesortes')

Thanks in advance
Theo

#1092816

Hello,

I suggest you try Types function types_render_field(), for example:

$latitude = types_render_field("geodaten", array(
	'post_id'	=> $result->ID,
	'format'	=> 'FIELD_LATITUDE'
));

$longitude = types_render_field("geodaten", array(
	'post_id'	=> $result->ID,
	'format'	=> 'FIELD_LONGITUDE'
));

More help:
https://toolset.com/documentation/customizing-sites-using-php/functions/#address

#1093053

Hi Luo,
thanks. Your suggestion works fine.

Thank you very much.
Kind regards
Theo

#1093412

You are welcome