1

I am trying to use a custom field value within a google api url.

I have 2 custom fields. I need to output the value of these custom fields within a url.

Custom field keys:

app_collection-postcode
app_delivery-postcode

This is the google url. I need to put the two postcodes in place of $postcode1 & $postcode2

$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=driving&language=en-EN&sensor=false";

I have tried the following which sort of worked. My problem with the below is I for some reason had 3333 appear on the page before the result of the echo and it is using arrays for single value keys.

$custom_fields = get_post_custom(the_ID());
$my_custom_field = $custom_fields['app_collection-postcode'];
$custom_fields2 = get_post_custom(the_ID());
$my_custom_field2 = $custom_fields2['app_delivery-postcode'];
$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$my_custom_field[0]&destinations=$my_custom_field2[0]&mode=driving&language=en-EN&sensor=false";
$data = @file_get_contents($url);
$result = json_decode($data, true);
foreach($result['rows'] as $distance) { 
    echo 'Distance from you: ' . $distance['elements'][0]['distance']['text'] . ' (' . $distance['elements'][0]['duration']['text'] . ' in current traffic)';
    }

Output of above:

3333Distance from you: 1.9 km (4 mins in current traffic) 

Is there a way to simplify the above as it seems bulky and I also am unsure why I am getting the 3333

3
  • It is being echo from somewhere else in your code!
    – Sumit
    Commented Apr 26, 2015 at 16:01
  • If I remove the $custom_fields bits of code the 3333 is no longer displayed.
    – Ben H
    Commented Apr 26, 2015 at 16:07
  • Got it typing a reply ;)
    – Sumit
    Commented Apr 26, 2015 at 16:10

1 Answer 1

2

You are using the_ID() instead of get_the_ID(). Which is being echo.

Also why you are using so many variable and pulling all the meta keys. I would like to suggest this code using get_post_meta().

$my_custom_field = get_post_meta(get_the_ID(), 'app_collection-postcode', true);
$my_custom_field2 = get_post_meta(get_the_ID(), 'app_delivery-postcode', true);
$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$my_custom_field&destinations=$my_custom_field2&mode=driving&language=en-EN&sensor=false";
$data = @file_get_contents($url);
$result = json_decode($data, true);
foreach($result['rows'] as $distance) { 
    echo 'Distance from you: ' . $distance['elements'][0]['distance']['text'] . ' (' . $distance['elements'][0]['duration']['text'] . ' in current traffic)';
}
1
  • Ahh perfect! didn't pick up on the get_the_ID bit! and I couldn't work out how to do it without the array, that was a perfect answer. Much appreciated Sumit
    – Ben H
    Commented Apr 26, 2015 at 16:19

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.