dpw50696 2017-01-13 12:29
浏览 63

ACF get_field()返回空

I'm developing an application through custom post type with custom fields using ACF plugin. When the custom post type is saved, it will create a blog in my network which works great however, when trying to copy the custom fields from the custom post type to the newly created blog's homepage with same acf custom fields, the acf custom fields are not saved.

I tested, $station_description = 'Not Empty'; and it does save/copied to its specific field without a problem. Its just that when i use, get_field() it does not save/copied to the new created blog.

What's wrong with my code?

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);
  • 写回答

1条回答 默认 最新

  • dqq9695 2017-01-14 05:30
    关注

    fount it.

    using get_post_meta to retrieve values is way better than get_field

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度