从单个 CSV 条目导入多个自定义字段值

Import multiple custom field values from single CSV entry

我在数据库中的数据是以数组的形式存储的。

正确的存储方式:

array (
  0 => '18459',
  1 => '18462',
)

导入的格式在我的 csv 文件中由竖线分隔,就像这样 18459|18461 但每当我导入时,它都会像这样存储值。

array (
  0 => '18459|18461',
)

[附加信息] 这是数据库通过复制元键 sp_team 来存储数据的方式。如果我可以将第二个元键更改为其他内容,例如 sp_team2 然后将其保存在我的 CSV 文件中,我认为它可能会起作用。问题是我的导入 认为 它应该只将两个 18459|18462 导入一个 sp_team field 并且不需要另一个 sp_team案子。但我不知道怎么办。

在两个字段的 wp-editor 中

如果有办法重命名第二个 sp_team 行得通吗?

我迫切需要解决方法。如您所见,它没有存储第二个值。我会感谢你的帮助。谢谢

您可以将钩子中的值分解并一个一个地插入。

这需要进入活动主题(或子主题)的 functions.php 文件。为清楚起见,内嵌注释。

function my_18459_update_post_meta( $post_id, $meta_key, $meta_value ) {
    $separator = '|';

    // only act on the 2 custom fields with pipes as separators:
    if($meta_key == 'sp_team' || $meta_key == 'sp_player') {

        /* Hook fires AFTER the "incorrect" unseparated meta value was 
           inserted in the DB, so need to delete it first: */
        delete_post_meta($post_id, $meta_key, $meta_value);

        // break up the values using | as separator:
        $meta_values = explode($separator, $meta_value);
        if ( ! empty( $meta_values ) ) {
            // insert values into wp_postmeta table one by one:
            foreach ( $meta_values as $meta_value ) {
                add_post_meta($post_id, $meta_key, $meta_value);
            }
        }
    }
}

add_action( 'pmxi_update_post_meta', 'my_18459_update_post_meta', 10, 3 );

我使用的相关导入设置截图:

您还可以在评论中提供的 link 检查导入设置。