Home › Forums › Backend Issues (wp-admin) › Dynamically populating repeater fields with default values? › Reply To: Dynamically populating repeater fields with default values?
Hey James, thanks for the help!
I had to make a tweak to your code to get it to work. I added a counter to modify the $value array instead of building it from scratch (otherwise it replaces any value the user sets with ‘ ‘).
function my_acf_set_repeater( $value, $post_id, $field ){
// echo '<pre>'; print_r($value); echo '</pre>';
// this one should consists array of the names
$settings_values = get_field('medication_types','option');
$i = 0;
foreach( $settings_values as $settings_value ){
$value[$i]['field_582e293d22e63'] = $settings_value['type'];
$i++;
}
//echo '<pre>'; print_r($value); echo '</pre>';
return $value;
}
add_filter('acf/load_value/name=medication', 'my_acf_set_repeater', 10, 3);
I realized, though, that using indexed repeater fields is not a smart way to do this—the client could mess it up by reordering the repeater fields.
I’ll probably redo it as a normal named field repeater, then set the field names as a prefixed version of the medication types fields… that way the client can’t reorder the fields, and they can change the medication type names without wrecking the fields.
Cheers!
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.