efreeman
efreeman

Reputation: 473

update_field() works when creating a post but get_field() returns false

I have three fields from advanced custom fields, these are postcode / latitude / longitude. The user only needs to enter the postcode in the field and when the post is created an action fires that runs a function that runs off to google and gets latitude / longitude and uses update_field() to set/update the values. I've checked in the database and the fields are definitely there. I've sanity checked that the field keys are the correct ones (checking through the ACF export script)

I am running update_field() with the field key (and not the field name), the values appear within the database paired up with the correct post ID, for all intents and purposes it looks like it has worked exactly as intended. The values display exactly as I'd expect to within the edit screen as well.

However, if I try to echo get_field or use the_field; it doesn't seem to work, where get_post_meta() does return a value.

function funky_post_updating_function($post_ID, $post, $update) {

// check we're looking at the right post type first, if not, get out
$post_type = get_post_type($post_id);
if ( "cpt_tutors" != $post_type ) return;

// latitude  acf-field_584548ec47d4e
// longitude acf-field_584548fa47d4f

$fields_to_watch = array(
    'field_584548ec47d4e', // latitude
    'field_584548fa47d4f'  // longitude
);

if( $postcode = get_field('postcode', $post_ID) ) : 

    $url = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($postcode)."&sensor=false&region=GB";

    $json = json_decode(file_get_contents($url));

    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $lng = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

    update_field('field_584548ec47d4e', $lat, $post_ID);
    update_field('field_584548fa47d4f', $lng, $post_ID);

else:

    foreach( $fields_to_watch as $field ):
        update_field($field, '', $post_ID);
    endforeach;

    return;

endif;

}

add_action( 'save_post', 'funky_post_updating_function', 10, 3 );

for reference, this is the fields as they appear in the ACF export:

'key' => 'field_584548ec47d4e',
'label' => 'Latitude',
'name' => 'latitude',

...

'key' => 'field_584548fa47d4f',
'label' => 'Longitude',
'name' => 'longitude',

So... why on earth would get_field() and the_field() not work?

edit: (I've discovered that using get_fields() is returning all of the values, so for now I'm just grabbing that lot but I'd still like to understand why this isn't working)

To clarify, this is the template usage of the code, this is in that stage where I'm just trying to get stuff to work rather than it being neat and tidy etc. This is part of a custom page template.

<?php

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'cpt_tutors',
    'post_status' => 'publish',
    'orderby' => 'post_title',
    'order' => 'ASC'
);

$tutors = get_posts( $args );

if( $tutors ) :

    $locations = array();

    echo '<ul>'."\n";

    // build our list of tutors!
    foreach( $tutors as $tutor ) :

        $id = $tutor->ID;

        echo '<li class="tutor-'.$id.'">';
        echo '<a href="'.get_the_permalink($id).'">'.get_the_title($id).'</a>';
        echo '</li>'."\n";

        // may as well grab some other bits and pieces whilst we're here, save doing the work later
        // (assuming these values are set?)

        if( $lat = get_field('latitude', $id) && $lng = get_field('longitude', $id) ) :

            array_push(
                $locations,
                array(
                    'name' => get_the_title($id),
                    'lat' => $lat,
                    'lng' => $lng
                )
            );

        endif;

    endforeach;

    echo '</ul>'."\n";

else :

    echo '<div>No tutors available</div>';

endif;

?>

Upvotes: 1

Views: 1626

Answers (1)

Prakash Sunuwar
Prakash Sunuwar

Reputation: 3

function funky_post_updating_function($post_ID, $post, $update) {

    // check we're looking at the right post type first, if not, get out
    $post_type = get_post_type($post_id);
    if ( "cpt_tutors" != $post_type ) return;

    // latitude  acf-field_584548ec47d4e
    // longitude acf-field_584548fa47d4f

    $fields_to_watch = array(
        'latitude', // latitude
        'longitude'  // longitude
    );

    if( $postcode = get_field('postcode', $post_ID) ) : 

        $url = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($postcode)."&sensor=false&region=GB";

        $json = json_decode(file_get_contents($url));

        $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
        $lng = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

        update_field('latitude', $lat, $post_ID);
        update_field('longitude', $lng, $post_ID);

    else:

        foreach( $fields_to_watch as $field ):
            update_field($field, '', $post_ID);
        endforeach;

        return;

    endif;
}
echo get_field('latitude').' '. get_field('longitude');

//or


$field = array(
    'type' => 'text',
    'name' => 'latitude',
    'key' => 'field_latitude',
);
$field = apply_filters('acf/load_field', $field, $field['key'] );
$field_value = apply_filters('acf/load_value', false, $post_id, $field);
echo $field_value;

$field = array(
    'type' => 'text',
    'name' => 'longitude',
    'key' => 'field_longitude',
);
$field = apply_filters('acf/load_field', $field, $field['key'] );
$field_value = apply_filters('acf/load_value', false, $post_id, $field);
echo $field_value;

Upvotes: 0

Related Questions