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