$upload_path, 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', $filename), 'post_content' => '', 'post_status' => 'inherit' ); // attaching the file to the post $attach_id = wp_insert_attachment($attachment, $upload_path, $post_id); // loading admin image library require_once(ABSPATH.'wp-admin/includes/image.php'); // genertating attachment metadata $attach_data = wp_generate_attachment_metadata($attach_id, $upload_path); // updating metadata on the attached image wp_update_attachment_metadata($attach_id, $attach_data); // adding attachment ID to the array of attached images IDs $attach_idz[] = $attach_id; } // updating ACF gallery with images IDs update_field($acf_key, $attach_idz, $post_id);