ACF get_field()返回空

时间:2017-01-13 12:29:50

标签: php wordpress advanced-custom-fields

我正在通过使用ACF插件的自定义字段自定义帖子类型开发应用程序。保存自定义帖子类型后,它会在我的网络中创建一个博客,但是当尝试将自定义字段从自定义帖子类型复制到具有相同acf自定义字段的新创建的博客主页时,acf自定义字段是没有保存。

我测试了$station_description = 'Not Empty';,它确实保存/复制到其特定字段而没有任何问题。只是当我使用时,get_field()它不会保存/复制到新创建的博客。

我的代码出了什么问题?

function myy_acf_save_post( $post_id ) {
    $post = get_post($post_id);   

    if( $post->post_type == 'network_directory' ) {

    $title = $post->post_title;
    $post_slug = $post->post_name;
    $client_username = get_field('client_username', $post_id);
    $station_server = get_field('station_server', $post_id);
    $station_location = get_field('station_location', $post_id);
    // $station_description = get_field('station_description', $post_id);
    $station_description = 'Not Empty';
    $language = get_field('language', $post_id);
    $station_email = get_field('station_email', $post_id);
    $station_website = get_field('station_website', $post_id);
    $station_port = get_field('station_port', $post_id);
    $station_logo = get_field('station_logo', $post_id);
    // $station_logo_url = wp_get_attachment_url( $station_logo );
    // $subdomain = get_field('subdomain', $post->ID);

    $main_site = 'domain.com';

    $subdomain_install = true;

    # Create a new user
    $check_user_id = get_user_id_from_string( $station_email );
    if ($check_user_id !== null) {
        $user_id = get_user_id_from_string( $station_email );
    } else {
        $rand_number = rand( 1, 2000 );
        $username = 'user-' . $rand_number;
        $password = wp_generate_password( 12, false );
        $email = $station_email;
        $user_id = wpmu_create_user( $username, $password, $email );
    }
    // wp_new_user_notification( $user_id, $password );

    # Create site
    if( $subdomain_install )
    {
        $newdomain = "{$post_slug}.$main_site";
        $path = '/';
    }

    $the_blog_id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' => 1 ) );


            if ( ! add_post_meta( $post_id, 'blog_id_meta_key', $the_blog_id, true ) ) { 
                update_post_meta( $post_id, 'blog_id_meta_key', $the_blog_id );
            }

            switch_to_blog( $the_blog_id );
                $homepage_id = get_option( 'page_on_front' );
                // $station_logo_new_src = media_sideload_image($station_logo_url, $homepage_id, 'Station Logo','src');
                // $station_logo_new_id = get_attachment_id_from_src($station_logo_new_src);
                update_field('client_username', $client_username, $homepage_id);
                update_field('station_server', $station_server, $homepage_id);
                update_field('station_location', $station_location, $homepage_id);
                update_field('language', $language, $homepage_id);
                update_field('station_email', $station_email, $homepage_id);
                update_field('station_website', $station_website, $homepage_id);
                update_field('station_port', $station_port, $homepage_id);
                // update_field('station_logo', $station_logo_new_id, $homepage_id);
                update_field('station_description', $station_description, $homepage_id);
            restore_current_blog();



    }   

}

add_action('acf/save_post', 'myy_acf_save_post', 9999);

2 个答案:

答案 0 :(得分:1)

在模板中扔print_r(get_post_meta(get_the_ID()))将输出可用的字段,这对调试很有用。就我而言,在页面上的模块重新排序后,我注意到get_post_meta的输出是意外的,这之后帮助我看到我忘记了使用wp_reset_postdata()在页面上方重新设置查询。

答案 1 :(得分:0)

计算它。

使用get_post_meta检索值比get_field

更好