Php update_field()在创建帖子时起作用,但get_field()返回false

Php update_field()在创建帖子时起作用,但get_field()返回false,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我有三个来自高级自定义字段的字段,它们是postcode/纬度/经度。用户只需在字段中输入邮政编码,当创建邮件时,将触发一个操作,该操作运行一个函数,该函数运行到google并获取纬度/经度,并使用update_field()设置/更新值。我已经检查了数据库,字段肯定在那里。我已经检查了字段键是否正确(通过ACF导出脚本检查) Iam使用字段键(而不是字段名)运行update_field(),数据库中的值与正确的post ID成对出现,无论出于何种目的,它看起来都完全按照预期工作。这些值的显示与

我有三个来自高级自定义字段的字段,它们是postcode/纬度/经度。用户只需在字段中输入邮政编码,当创建邮件时,将触发一个操作,该操作运行一个函数,该函数运行到google并获取纬度/经度,并使用update_field()设置/更新值。我已经检查了数据库,字段肯定在那里。我已经检查了字段键是否正确(通过ACF导出脚本检查)

Iam使用字段键(而不是字段名)运行update_field(),数据库中的值与正确的post ID成对出现,无论出于何种目的,它看起来都完全按照预期工作。这些值的显示与我在编辑屏幕中预期的完全相同

但是,如果我尝试回显get_字段或使用_字段;它似乎不起作用,其中get\u post\u meta()会返回一个值

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 );
以下是显示在ACF导出中的字段,以供参考:

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

所以。。。究竟为什么会得到_场()而_场()不起作用

编辑:(我发现使用get_fields()会返回所有的值,所以现在我只是获取这些值,但我仍然想理解为什么这不起作用)

要澄清的是,这是代码的模板用法,这是在那个阶段,我只是试图让东西工作,而不是整洁等。这是自定义页面模板的一部分

<?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;

?>

函数funky\u post\u update\u函数($post\u ID,$post,$update){


函数funky\u post\u updatement\u函数($post\u ID,$post,$update){


请小心,因为您在函数中同时使用了
$post\u id
$post\u id
变量,PHP变量名区分大小写!另外,您能否添加一些代码,说明如何以及在何处调用
get\u field()
the\u field()
?很好,我已经编辑了这个问题,添加了代码示例,也是关于:区分大小写的变量,我怎么错过了?!谢谢你的提示!小心点,因为你在函数中同时使用了
$post\u id
$post\u id
变量,PHP变量名是区分大小写的!另外,你能添加一些代码来说明如何和你在这里调用
get_field()
the_field()
?很好,我已经编辑了这个问题来添加代码示例,还有关于:区分大小写的变量,我怎么会错过它?!谢谢你的关注!
<?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;

?>
    // 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;