I have a custom post type for addresses on a Google Map. One meta field is for the address, but I want to convert that address to longitude/latitude. So I have a meta field for longitude & one for latitude.

I created a function in my loop to update the longitude & latitude postmeta based on the address postmeta. Here is the function:

function geocode_spot_address ($post_ID) {
    $address = get_post_meta($post_ID,'spots_address',true);

    $address = str_replace(' ', '+', $address);
    $address = 'http://maps.google.com/maps/geo?q=1020+' . $address . '&output=xml';
    $result = file_get_contents($address);
    $xml = new SimpleXMLElement($result);

    list($longitude, $latitude, $altitude) = explode(",",$xml->Response->Placemark->Point->coordinates);

    update_post_meta($post_ID, 'spots_longitude', $longitude);
    update_post_meta($post_ID, 'spots_latitude', $latitude);

    return $post_ID;     

This works great when I refresh the page that I call the function on in the loop and pass the post ID:


For the life of me though, I can't get this to work when trying to get it to update longitude/latitude on post save/publish/update by hooking the function to:

add_action('save_post', 'geocode_spot_address', 1, 2);

I obviously just want to enter the street address, then on save/publish/update have it run this function and add longitude/latitude to the appropriate meta fields.

Any help would be appreciated.

FWIW I am using Rilwis's Meta Box framework for creating my custom post types and meta boxes.

  • Can you try ending the function with die() and check the DB updates after the save_post ends? I think there might be a caching bug with update_post_meta, I reported it few weeks back but no response so far - core.trac.wordpress.org/ticket/20690 . If you consider this similar, you could comment there too. – Mario Peshev Jul 28 '12 at 12:30
  • I do in fact get the same result. If I kill the function before it ends, the data is entered into the database. But as suspected, if I step out of the Rilwis Meta Box framework I am using, I can make the function work. This leads me to believe it is likely a nonce issue, or something of the like simply not validating the update_post_meta. – 3oh6 Jul 31 '12 at 2:32

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.