Home › Forums › Add-ons › Repeater Field › Dynamically Populate Repeater Field with Users › Reply To: Dynamically Populate Repeater Field with Users
It is possible to create each row individually, but for your purposes, it sounds like you want to create them once for this repeater and never update them again.
I find it much easier to create the entire repeater in one go and on top of this, you can’t have different values automatically generated for each row of the repeater. For example, you can’t use an acf/load_value filter on one of the sub fields, the value generated for this sub field will be the same for every row you generate. The only real way to dynamically generate these values and have a different value for each row is to do them all at once.
Also, when dealing with fields that do not already exist it is always better to use field keys. 99% of the time if you use the field name for fields that have not already been saved then you won’t get the results your looking for.
So, what you need to do is add an acf/load_field filter for the repeater field.
add_filter('acf/load_value/key=field_123456', 'editor_commission_repeater_values', 20, 3);
function my_repeater_values($value, $post_id, $field) {
// If the value of this field has never been set then it will === false;
if ($value !== false) {
// the value is not false, so it has already been saved
// do not regenerate this field
return $value;
}
// get all editors
// only get the user ID because this is what
// ACF needs to store for a user type field
$args = array(
'role' => 'editor',
'fields' => 'ID'
);
$users = get_users($args);
// make sure we got some users
if (!$users) {
// if not then no need to continue
return $value;
}
// generate rows of the repeater
// initialize array
$value = array();
foreach ($users as $user) {
// add a row to the value for this user
$value[] = array(
// the first field is the user field
// use the field key
'field_567890' => $user,
// the second field is a number field, but could be anything
'field_876543' => 100 // assuming number field and default value
);
}
// return the generated repeater
return $value;
}
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.