【问题标题】:ACF get_field() returns emptyACF get_field() 返回空
【发布时间】:2017-05-28 19:56:33
【问题描述】:

我正在使用 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);

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      找到它。

      使用get_post_meta 检索值比get_field 好得多

      【讨论】:

      • 你能澄清一下吗?您找到使用 get_post_meta() 检索字段内容的方法了吗?
      猜你喜欢
      • 2019-11-03
      • 2014-09-13
      • 2019-11-16
      • 2020-03-04
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      相关资源
      最近更新 更多