Support

Account

Home Forums General Issues Save Google Map field data separately instead of serialized array

Solving

Save Google Map field data separately instead of serialized array

  • I currently have a repeater field saved to each user, with a Google Map field inside it, allowing users to set multiple locations for where they operate from.

    Now I’m trying to run a WP User Query using Geo Query (https://github.com/birgire/geo-query), but the Google Map field is saved as a serialised array, how would I go about saving the lat and lon of each row in the repeater to a custom meta key so they can be searched?

  • Use an acf/save_post filter, get the values of the repeater/sub fields (unfomatted) and save them as a standard WP meta field

    
    add_action('acf/save_post', 'gmf_to_standard_wp', 20);
    function gmf_to_standard_wp($post_id) {
      // make sure a user is being saved
      if (substr($post_id, 0, 5) != 'user_')) {
        return;
      }
      // get the user id
      $user_id = intval(substr($post_id, 5);
      // delete existing post meta so that we start fresh
      delete_user_meta($user_id, 'user_lon');
      delete_user_meta($user_id, 'user_lat');
      // loop over rows and save values
      if (have_rows('your-repeater', $post_id)) {
        while (have_rows('your-repeater', $post_id)) {
          the_row();
          // get value unformatted
          $data = get_sub_field('your-google-map-field', false);
          // you will need to excuse me here because I
          // don't know how this array is saved by ACF
          // or what the indexes are, so you will need to figure
          // this out. I am only guessing at "lon" and "lat"
          // add each value allowing multiple values
          add_user_meta($user_id, 'user_lat', $data['lat'], false);
          add_user_meta($user_id, 'user_lon', $data['lon'], false);
        }
      }
    }
    
  • Thanks John, I’ll give this a go and get back with results!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.