Support

Account

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!