Support

Account

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.