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

时间:2016-12-14 10:37:19

标签: php wordpress advanced-custom-fields

我有三个来自高级自定义字段的字段,这些字段是邮政编码/纬度/经度。用户只需要在字段中输入邮政编码,并且当创建帖子时,会触发一个动作,该动作运行一个运行到谷歌并获取纬度/经度的函数,并使用update_field()来设置/更新值。我已经检查了数据库,这些字段肯定在那里。我理智地检查了字段键是否正确(通过ACF导出脚本检查)

使用字段键(而不是字段名称)运行update_field(),这些值显示在数据库中,并与正确的帖子ID配对,所有意图和目的看起来像它完全符合预期。这些值也完全按照我在编辑屏幕中所期望的那样显示。

但是,如果我尝试回显get_field或使用the_field;它似乎不起作用,get_post_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',

...

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

那么......为什么地球上的get_field()和the_field()不起作用?

编辑:(我发现使用get_field s () 会返回所有值,所以现在我只是抓住那个很多,但我仍然想知道为什么这不起作用)

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

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

?>

1 个答案:

答案 0 :(得分:0)

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;