Home › Forums › ACF PRO › Change Select Default Value for each Repeater Item › Reply To: Change Select Default Value for each Repeater Item
Altering fields on different rows of a repeater is extremely difficult.
For existing fields, for example if a repeater already has 4 rows then this can be done using an acf/prepare_field filter. The filter needs to keep track of how many rows have been shown and then set a different value or default value based on the counter.
// field_XXXXXXX represents field key of sub field
add_filter('acf/prepare_field/key=field_XXXXXXX', 'prepare_my_subfield');
function prepare_my_subfield($field) {
static $counter = 0
$values = array_keys($field['choices']);
if (isset($values[$counter])) {
$field['default_value'] = $values[$counter];
}
$counter++;
return $field;
}
For fields that do not already exist, for example when adding a row then you need to use JavaScript https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
You need to add an JS acf append action and set the selected value of the field using the ACF JS API. Sorry, I don’t have code for this, it is more difficult for me to churn that out.
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.