从 CSV 导入 ACF 转发器字段值

Import ACF Repeater Field Values from CSV

我正在尝试使用文件字段 (dd_csv) 将数据加载到转发器字段 (mortgage_providers) 的子字段中,以将文件上传到 post 和 return 一个数组。然后用 PHP 遍历每一行并更新转发器字段。我尝试了以下方法,但似乎无法正常工作。

      if( get_field('dd_csv') ) {
        // load csv with SERVER PATH instead of URL
        $csv = get_attached_file(get_field('dd_csv')['id']);
        if(($handle = fopen($csv, "r")) !== FALSE) {
          $count = 0;
          while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            // $count = acf row, $data[0] = csv column 1 value
            update_sub_field(array('mortgage_providers', $count, 'mortgage_provider_name'), $data[0], 'option');
            $count++;
          }

          fclose($handle);
        }
      }

这是数据示例:

CSV 的第 1 列中总共有 1000 多行。

谢谢

我会挂钩 on save action:

<?php

add_action( 'save_post', 'prefix_check_files', 10, 3 );

function prefix_check_files( $post_id, $post, $update ) {
if( get_field('dd_csv', $post_id) ) {
  // load csv with SERVER PATH instead of URL
  $csv = get_attached_file(get_field('dd_csv', $post_id)['id']);
  if(($handle = fopen($csv, "r")) !== FALSE) {
    $count = 0;
    while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
      // $count = acf row, $data[0] = csv column 1 value
      update_sub_field(array('mortgage_providers', $count, 'mortgage_provider_name'), $data[0], 'option');
      $count++;
    }
    fclose($handle);
  }
}
}