Home › Forums › Add-ons › Repeater Field › I need help populating repeater field with WP All Import › Reply To: I need help populating repeater field with WP All Import
The solution that I am posting actually works for me!
I will post more info about just for anyone that might need it in the future.
WP All Import cannot import multiply values to a ACF Repeater field if the values are on multiply rows on the xml sheet.
As the WP All Import authors suggest there is a workaround for that. You can find it here https://www.wpallimport.com/documentation/developers/code-snippets/#append-acf-repeater-data.
Unfortunately, the above workaround works only if there is one sub-field in the Repeater field.
So, for my case where the sub-fields are 4, I need to work on another solution.
The solution is quite simple if you understand the workaround. You need to create a custom field for each ACF Repeater sub-field.
For my example my 4 subfields are car_make, car_model, car_year and car_type (you need to use field name as shown on ACF on WP admin panel). So I need to create 4 different custom fields, one for each ACF Repeater sub-field.
I created the my_repeater_data_car_make, my_repeater_data_car_model, my_repeater_data_car_year and my_repeater_data_car_type.
Then on the import process I used the custom fields option in order to link the data from my xml to the custom fields, please check here https://prnt.sc/19ray48
Then I just added the below code in the functions editor.
add_action( 'pmxi_saved_post', 'soflyy_add_data', 10, 3 );
function soflyy_add_data( $id, $xml, $update ) {
$selector = 'car_make_model_year'; // Parent field name
$subfield1 = 'car_make'; // The repeating field you want to add the first value to
$subfield2 = 'car_model'; // The repeating field you want to add the second value to
$subfield3 = 'car_year'; // The repeating field you want to add the third value to
$subfield4 = 'car_type'; // The repeating field you want to add the forth value to
if ( $value1 = get_post_meta( $id, 'my_repeater_data_car_make', true )) {
$value2 = get_post_meta( $id, 'my_repeater_data_car_model', true );
$value3 = get_post_meta( $id, 'my_repeater_data_car_year', true );
$value4 = get_post_meta( $id, 'my_repeater_data_car_type', true );
$row = array( $subfield1 => $value1, $subfield2 => $value2, $subfield3 => $value3, $subfield4 => $value4, );
add_row( $selector, $row, $id );
}
delete_post_meta( $id, 'my_repeater_data_car_make' );
delete_post_meta( $id, 'my_repeater_data_car_model' );
delete_post_meta( $id, 'my_repeater_data_car_year' );
delete_post_meta( $id, 'my_repeater_data_car_type' );
}
Run the import and that’s all.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.