php – Problems updating nested ACF field groups

Question

I have a frontend form for creating custom posts that accepts data for all ACF associated with that post type. On submission of the form, I am successfully able to create the new post along with pushing normal ACF fields values.

The problem is with the ‘Group’ ACF field called ‘Working Hours’ which further has 7 Groups inside it, each named after every day of the week: ‘Monday’, ‘Tuesday’… And each day group contains 2 text fields: am, pm, and a third checkbox called ‘closed‘ which the member will select if that is a weekly off. So it is like this:

#Working Hours( Group )#

——-##Monday ( Group )##

———— ###am (text)###
———— ###pm (text)###
———— ###closed (checkbox)###

I am getting an error undefined index: sub_fields...
And this is my code that I am trying to update the fields:

// Grabbing the Working Hours Group field
$hours = get_field( 'field_61f453c010074', $post_id );

foreach ( $hours['sub_fields'] as $key => $value ) { // error in sub_fields in this line
    foreach ( $value['sub_fields'] as $timing ) {
        $timing[0] = $_POST['field_61f4573589282'];// am
        $timing[1] = $_POST['field_61f4574589283'];// pm
        $timing[2] = $_POST['field_61f7a28ffbcef'];// 'Closed' checkbox
    }
}

update_field( 'field_61f453c010074', $value, $post_id ); // error here too value is undefined

This is how the Working Hours Group field is printed. Here, I have posted the data returned for the parent group, as well as taken only the first child for sample i.e. Monday:

Array
        (
            [ID] => 48
            [key] => field_61f453c010074
            [label] => Hours
            [name] => hours
            [prefix] => acf
            [type] => group
            [value] => 
            [menu_order] => 9
            [instructions] => 
            [required] => 0
            https://wordpress.stackexchange.com/q/402110 => 
            [class] => 
            [conditional_logic] => 0
            [parent] => 36
            [wrapper] => Array
                (
                    [width] => 
                    [class] => 
                    https://wordpress.stackexchange.com/q/402110 => 
                )

            [layout] => block
            [sub_fields] => Array
                (
                    [0] => Array
                        (
                            [ID] => 49
                            [key] => field_61f4570e89281
                            [label] => Monday
                            [name] => monday
                            [prefix] => acf
                            [type] => group
                            [value] => 
                            [menu_order] => 0
                            [instructions] => 
                            [required] => 0
                            https://wordpress.stackexchange.com/q/402110 => 
                            [class] => 
                            [conditional_logic] => 0
                            [parent] => 48
                            [wrapper] => Array
                                (
                                    [width] => 
                                    [class] => day
                                    https://wordpress.stackexchange.com/q/402110 => 
                                )

                            [layout] => block
                            [sub_fields] => Array
                                (
                                    [0] => Array
                                        (
                                            [ID] => 50
                                            [key] => field_61f4573589282
                                            [label] => am
                                            [name] => am
                                            [prefix] => acf
                                            [type] => number
                                            [value] => 
                                            [menu_order] => 0
                                            [instructions] => Only enter numbers
                                            [required] => 0
                                            https://wordpress.stackexchange.com/q/402110 => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => am
                                                    https://wordpress.stackexchange.com/q/402110 => 
                                                )

                                            [default_value] => 
                                            [placeholder] => 
                                            [prepend] => 
                                            [append] => 
                                            [min] => 
                                            [max] => 
                                            [step] => 
                                            [_name] => am
                                            [_valid] => 1
                                        )

                                    [1] => Array
                                        (
                                            [ID] => 51
                                            [key] => field_61f4574589283
                                            [label] => pm
                                            [name] => pm
                                            [prefix] => acf
                                            [type] => number
                                            [value] => 
                                            [menu_order] => 1
                                            [instructions] => Only enter numbers
                                            [required] => 0
                                            https://wordpress.stackexchange.com/q/402110 => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => pm
                                                    https://wordpress.stackexchange.com/q/402110 => 
                                                )

                                            [default_value] => 
                                            [placeholder] => 
                                            [prepend] => 
                                            [append] => 
                                            [min] => 
                                            [max] => 
                                            [step] => 
                                            [_name] => pm
                                            [_valid] => 1
                                        )

                                    [2] => Array
                                        (
                                            [ID] => 52
                                            [key] => field_61f7a28ffbcef
                                            [label] => closed
                                            [name] => closed
                                            [prefix] => acf
                                            [type] => checkbox
                                            [value] => 
                                            [menu_order] => 2
                                            [instructions] => 
                                            [required] => 0
                                            https://wordpress.stackexchange.com/q/402110 => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => closed
                                                    https://wordpress.stackexchange.com/q/402110 => 
                                                )

                                            [choices] => Array
                                                (
                                                    [Yes] => Yes
                                                )

                                            [allow_custom] => 0
                                            [default_value] => Array
                                                (
                                                )

                                            [layout] => vertical
                                            [toggle] => 0
                                            [return_format] => value
                                            [save_custom] => 0
                                            [_name] => closed
                                            [_valid] => 1
                                        )

                                )

                            [_name] => monday
                            [_valid] => 1
                        )

0
Mr.CODER 7 months 2022-02-01T16:30:27-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse