4

I use Advanced Custom Fields (ACF) to store data in a custom field. I had latitude and longitude information stored separately, but I they need to be in one custom field so I made the function below. $query just queries all the relevant posts.

<?php 
$query = query_custom_posts(); if($query->have_posts()): 
while ($query->have_posts()): $query->the_post();
if(get_field('map_lat') && get_field('map_lng')):

$lat = get_field('map_lat');
$long = get_field('map_lng');
$coord = $lat.','.$long;
$post_id = get_the_ID();

update_post_meta($post_id, 'map_coor', $coord);


endif; endwhile; wp_reset_query(); endif;

?>

Now the function above works. The field map_coor gets populated with the lat and long separated by a comma (I can see the information in the field).

The weird thing is that if I use get_field('map_coor') on the front-end nothing shows up. If I save the corresponding post, then the info DOES show up.

In the function above I also tried to use the function update_field (from ACF) like this: update_field($post_id, 'map_coor', $coord) but the problem is still there.

So my question is; do I need to alter the function above or just run a function which saves all posts? If the latter is the case; how do I do that? I've found the hook save_posts but I'm not sure how to let it run through all the posts.

10

The problem is that the function update_field from ACF works a little bit different from update_post_meta. Can you see it?

update_post_meta( $post_id, $meta_key, $meta_value, $prev_value );
update_field( $field_key, $value, $post_id );

If you use update_post_meta you should in my opinion use get_post_meta to get the meta. If you use update_field you should use the_field or get_field because ACF stores the key/values-pairs in a slightly different way.

  • 1
    If you use update_field you must use the_field or get_field because ACF stores the key/values-pairs in a slightly different way this is not entirely accurate. it's true that ACF stores values differently, but you can use update_field and retrieve the contents with get_post_meta just fine as long as you use update_field correctly. the important thing to note is that $field_key is not the same thing as $meta_key! – Milo Nov 1 '13 at 15:02
  • I changed that to "should in my opinion use". What you said is absolute correct but this "field-name/field-reference"-thing is very often the problem. – realloc Nov 1 '13 at 15:06
  • Thanks. Like I've described in my question, I've tried update_field. I've used it in combination with get_field. That gives the same outcome; the field value is saved, but when I query the custom field on the front-end it returns empty. I'll try update_post_meta and replace get_field with get_post_meta. – eskimo Nov 1 '13 at 17:18
  • 1
    It does work when I use update_post_meta and get_post_meta. Still think it's strange that update_field and get_field don't give the same results. I'll mark the answer as accepted, thanks again. – eskimo Nov 1 '13 at 17:35
  • I am using update_post_meta() and get_field() and it is working -- `update_field() was not working but I still don't know why. – Alex Steinberg Jun 25 '18 at 16:30

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.