Question:

Programatically import custom fields using wp all import

Eleanor: 2 weeks ago

I have a csv with a lot of custom fields that I need to import. It would take too much time to manually set up the fields using the interface and it also is not very client friendly to ask them to make sure the values match up. Many of the values must also be mapped from the csv to a different value in the custom field in WordPress.

Answer:
Benjamin: 2 weeks ago

WP All Import has a lot of undocumented features in the various hooks, actions and filters implemented in the code. The easiest way to do such a mapping is to add a filter to the pmxi_options_options and add custom rules. These rules will show up under the Custom Fields accordion during the import configuration step.

<?php
add_filter( 'pmxi_options_options', function( $options ) {
  // Match the desired custom post type
  if ( $options['custom_type'] != 'product' ) {
    return $options;
  }
  // Configure the custom fields
  $options['custom_name']['custom_field_name'] = '_custom_field_name';
  $options['custom_value']['custom_field_name'] = '{csv_column[1]}';
  $options['custom_mapping_rules']['custom_field_name'] = json_encode( [
    'value_from_csv' => 'value_to_custom_field',
    'en' => 'English',
  ] );
  return $options;
} );

enter image description here