lukeseager
lukeseager

Reputation: 2615

ACF Update Field into nested repeater field

I'm trying to let users post from the front end into a nested ACF repeater field.

I've got the sub fields in the first repeater posting fine. But, I can't quite figure out how to get the nested repeater working properly.

Here's the code I have so far:

$event_field_key = 'field_535e6b9ffe3da';

$events[] = array(
    'start-date'   => $startDate,
    'end-date'     => $endDate,
    'notes'          => $_POST['p'.$p.'-notes'],
    'start-end-times' => array(
        'start-time' => '09:00', // would be dynamic
        'end-time' => '17:00' // would be dynamic
    )
);

update_field($event_field_key, $events, $post_id);

I'm not sure if I can just nest another array in there, or if I need to do something else.

[UPDATE]

I've just done this and it does input into the first row:

$event_field_key = 'field_535e6b9ffe3da';

$events[] = array(
    'start-date'   => $startDate,
    'end-date'     => $endDate,
    'notes'          => $_POST['p'.$p.'-notes'],
    'start-end-times' => array(
       'start-time' => '9:00',
       'end-time' => ' 17:00'
    )
);

update_field($event_field_key, $events, $post_id);

However, this code puts row 1 values both as 9 and row 2 values as 1.

So it looks like:

Row 1: start time: 9, end time: 9 Row 2: start time: 1, end time: 1

I can't seem to find any documentation on this, but it looks like it's possible, just a case of figuring out the syntax.

Upvotes: 2

Views: 12134

Answers (1)

lukeseager
lukeseager

Reputation: 2615

The fix was an array of arrays:

$event_field_key = 'field_535e6b9ffe3da';

        $events[] = array(
            'start-date'   => $startDate,
            'end-date'     => $endDate,
            'notes'          => $_POST['p'.$p.'-notes'],
            'start-end-times' => array(
                array(
                    'start-time' => '09:00',
                    'end-time' => '17:00'
                ),
                array(
                    'start-time' => '10:00',
                    'end-time' => '16:00'
                )
            )
        );

        update_field($event_field_key, $events, $post_id);

Upvotes: 4

Related Questions