Support

Account

Home Forums Add-ons Repeater Field Repeater inside a group

Solving

Repeater inside a group

  • Hey all!

    I have the following ACF structure.
    A group with a repeater inside. The repeater also has two subfields, a text and time field

    Group
    > repeater
    >> Text – subfield
    >> Time – subfield

    I’m trying to add multiple rows of the repeater with an event schedule.

    
    $group_key = 'field_64e765bfc42ef';
    
        foreach ($event->lineup as $item) {
            $value = [];
            $value = array(
    
                'field_64e76f9210338' => array(
                   
                    array(
                        'field_64e770e717754' => $item->details,
                        'field_64e770f317755' => $item->time
                    ),
                ),
            );
    
            update_field($group_key, $value, $post_id);
        }
    

    The data in the API is structures as follows:

    
    "lineup": [
    {
    "details": "Doors open",
    "time": "9:00 PM"
    },
    {
    "details": "Last Entry",
    "time": "11:00 PM"
    },
    {
    "details": "Curfew",
    "time": "11:00 PM"
    }
    ],
    

    For each repeater row. I’m trying to output every “details” and “time” inside the “lineup” array. The output would be something like the image attached image.

    Is it possible to set up a repeater this way inside a group? I’ve tried a few different things but I only get one of the items in the line up array to output in the repeater. I’m not sure if this is possible, or if it’s something to do with the syntax etc. Hope this is making sense. Any help would be greatly appreciated.

    Thanks
    Simon

  • Hi Simon,

    I guess ‘field_64e76f9210338’ is your group field key.
    If that’s the case, i don’t think you need it if you only want to update your repeater inside your group.

    Maybe you could try something like the code below.
    Let me know if it helps you.

    Cheers

    <?php
    //be sure that's your repeater key
    $repeater_key = 'field_64e765bfc42ef';
    $repeater_name = 'your_rep_name';
    
    //$rep_count will be used to create a new repeater OR add rows to an existing repeater
    $rep_count = count(get_field($repeater_name)); //not sure if it works with field key
    
    foreach ($event->lineup as $item) {
    	$updated_rep = []; //Not sure if this is needed - it won't hurt
    	
    	//Create an entirely new repeater
    	if($rep_count === 0) {
    		$new_rep = array(
    			array(
    				'field_64e770e717754' => $item->details,
    				'field_64e770f317755' => $item->time
    			),
    		);
    		update_field( $repeater_key, $new_rep, $post_id );
    	} 
    	//Or keep previous values and add new rows
    	elseif($rep_count >= 1) {
    		$new_row = array(
    			'field_64e770e717754' => $item->details,
    			'field_64e770f317755' => $item->time
    		);
    		add_row($repeater_key, $new_row, $post_id);
    	}
    }
  • In both sets of code you are resetting the value to an empty array and then updating the field on each loop. This means that only the last value will exist when done.

    You need to build the repeater and update with all rows at the same time

    going by your first code

    
    $group_key = 'field_64e765bfc42ef';
    $repeater_key = 'field_64e76f9210338';
    $group_value = array();
    $repeater_value = array();
    foreach ($event->lineup as $item) {
      // add row to repeater
      $repeater_value[] = array(
        'field_64e770e717754' => $item->details,
        'field_64e770f317755' => $item->time
      );
    } // end foreach row
    $group_value = array(
      $repeater_key => $repeater_value
    );
    

    Now you can update the group field with the group value.

    Also, do not forget that if the group, or the repeater has any values other than these fields then you will need to get the current value of the group field and modify that existing value instead of starting with nothing.

    To get the value of the existing group before you updated in

    
    if (have_rows('group_field_name')) {
      // group field always true
      while (have_rows('group_field_name')) {
        // always happens 1 time for group fields
    
        // the row returns an array with field key => unformatted value pairs
        // ready for use in update_field();
        $group_field_value = the_row();
      }
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.