1

I'm having an issue with a function i'm trying to create. It should be simple. When someone saves a post, grab the city/address/province and convert it to long/lat then save that to the database. The problem is, I ca not get "update_post_meta" to work, and I don't understand why.

The code is below, it's simple enough.

add_action( 'save_post_dealer', 'save_dealer_long_lat');
function save_dealer_long_lat($post_id) {
    $city = get_post_meta( $post_id, 'wpcf-dealer-city', true );
    $address = get_post_meta( $post_id, 'wpcf-dealer-address', true );
    $province = get_post_meta( $post_id, 'wpcf-dealer-province', true );
    $coords = get_coordinates($city, $address, $province);

    $lat = get_post_meta( $post_id, 'wpcf-latitude', true );
    $long = get_post_meta( $post_id, 'wpcf-longitude', true );

    if(empty($lat)) {
        update_post_meta($post_id, 'wpcf-latitude', $coords['lat'], $lat);
    }
    if(empty($long)) {
        update_post_meta($post_id, 'wpcf-longitude', $coords['long'], $long);
    }
}

a few things to eliminate the obvious mistakes.

  • yes, the post type is 'dealer' - this function is being run correctly.
  • Each of the variables is set properly, I confirmed with a var_dump that they were all set and giving the correct details.
  • the get_coordinates function is also working correctly and is giving the proper return.
  • the update_post_meta is being reached - the lat/long are empty (no entry for it even exists in the DB)

I don't understand whats going on, everything I see seems to suggest this should be working.

  • 1
    The 4th argument is $prev_value. If $lat is empty, then my guess is that there is no previous value. What if you just try update_post_meta($post_id, 'wpcf-latitude', $coords['lat']) and update_post_meta($post_id, 'wpcf-longitude', $coords['long']); – czerspalace Apr 7 '17 at 18:49
  • I just tried with a removed $lat and $long from the update_post_meta - there is no change in behaviour. – Jeremiah Apr 7 '17 at 18:54
  • What do you get if you var_dump($lat) and var_dump($long) – czerspalace Apr 7 '17 at 19:12
  • $lat and $long return: string(0) "" – Jeremiah Apr 7 '17 at 19:19
  • And what does var_dump($coords) look like? – czerspalace Apr 7 '17 at 19:24
1

A little late, but I just had this issue where adding a taxonomy term was working, but adding post meta wasn't. The solution was to set a higher priority. The default priority is 10, so try for instance priority of 100:

add_action( 'save_post_dealer', 'save_dealer_long_lat', 100, 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.