Home › Forums › Add-ons › Repeater Field › acf/input/form_data conflicting with DB query › Reply To: acf/input/form_data conflicting with DB query
You’re Brilliant @hube2! This fixed it!
Here is a simplified version of how the code worked:
function update_sub_fields() {
global $wpdb; //declare using a global variable
$import_post_id = get_field('import_post'); //get the id of the post we are importing data from
if( have_rows('repeater_field_name') ): //start the repeater loop
while( have_rows('repeater_field_name') ): the_row();
$unique_id = get_sub_field('unique_id_field'); //get the unique id
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM {$wpdb->prefix}postmeta
WHERE post_id = %s
AND meta_key LIKE %s
AND meta_value = %s
",
$import_post_id, //only look at meta-values that match the post we are importing from
'import_repeater_field_%_unique_id_field', // meta_name: $ParentName_$RowNumber_$ChildName
$unique_id // meta_value: to match our unique id
));
if( $rows ){
foreach( $rows as $row ){
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$r = $matches[0];
$import_rows = get_field('import_repeater_field', $import_post_id);
$current_import_row = $import_rows[$r];
$sub_field_to_import = $current_import_row['sub_field_to_import'];
update_sub_field('new_field_to_update', $sub_field_to_import);
} //End foreach
} //End if
endwhile; //end loop
endif;
}
add_action( 'acf/imput/form_data', 'update_sub_fields', 10, 1);
I hope this helps anyone else who might be looking to import data from one repeater field to another!
Jess
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.