Support

Account

Home Forums ACF PRO Adding rows in a repeater field while in a For loop Reply To: Adding rows in a repeater field while in a For loop

  • Hi @jamesgeorgedunn

    It seems your code is creating a new value and replacing the repeater field for every image you have. You should do it like this instead:

    $arrayCount = count($feed['Images']);
    
    // set the variables first
    $field_name = 'repeater_field_name';
    $field_key = 'field_583ea1bfe02e9';
    
    // get the existing value
    $value = get_field($field_name, $propertyID);
    
    // add the new values
    for($i = 0; $i < $arrayCount; $i++) {
    	$value []= array(
            'order'	=> $feed['Images'][$i]['Order'],
            'primary_image'	=> $feed['Images'][$i]['IsPrimaryImage'],
            'id' => $feed['Images'][$i]['Id'],
            'url' => $feed['Images'][$i]['Url'],
            'document_type'	=> $feed['Images'][$i]['DocumentType']['DisplayName'],
            'document_sub_type'	=> $feed['Images'][$i]['DocumentSubType']['DisplayName'],
        );
    	
    }
    
    // update it
    update_field($key, $value, $propertyID);

    Don’t forget to change the “repeater_field_name” value with your repeater field name.

    I hope this helps 🙂