Lets import data from a csv file. Dowload sample csv file here. The fields in csv can be edited according to requirement.
Now to read the data from csv
$data = csv_to_array(get_template_directory() . "/sample.csv", $delimiter = ',');
Import data into ACF fields for new post
Reading the record from $data variable and inserting into Acf Field.
foreach ($data as $key => $value) { // getting values from user $coursename = $value['Course Name']; $courseType = $value['cost']; $instructorName = $value['Instructor Name']; $instructorProfile = $value['Instructor Profile']; $instructorImage = $value['Instructor Image']; // inserting into post $post_id = wp_insert_post(array( 'post_status' => 'publish', // It can be draft,private as well 'post_type' => 'courses', // name of custom post type,default is post 'post_title' => $coursename )); if($post_id){ // post is being created // insert data into ACF Radio Button Field update_post_meta($post_id, 'cost', $cost); // insert data into ACF Text Field update_post_meta($post_id, 'instructor_name', $instructorName); // insert data into ACF Link Field update_post_meta($post_id, 'instructor_profile', $instructorProfile); // insert data into ACF Image Field update_post_meta($post_id, 'instructor_image', $instructorImage); } }
Do Let me know in comments if there is any issue. Happy to help 🙂