php - Problems updating nested ACF field groups

admin2025-01-08  4

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
            [id] => 
            [class] => 
            [conditional_logic] => 0
            [parent] => 36
            [wrapper] => Array
                (
                    [width] => 
                    [class] => 
                    [id] => 
                )

            [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
                            [id] => 
                            [class] => 
                            [conditional_logic] => 0
                            [parent] => 48
                            [wrapper] => Array
                                (
                                    [width] => 
                                    [class] => day
                                    [id] => 
                                )

                            [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
                                            [id] => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => am
                                                    [id] => 
                                                )

                                            [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
                                            [id] => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => pm
                                                    [id] => 
                                                )

                                            [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
                                            [id] => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => closed
                                                    [id] => 
                                                )

                                            [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
                        )

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
            [id] => 
            [class] => 
            [conditional_logic] => 0
            [parent] => 36
            [wrapper] => Array
                (
                    [width] => 
                    [class] => 
                    [id] => 
                )

            [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
                            [id] => 
                            [class] => 
                            [conditional_logic] => 0
                            [parent] => 48
                            [wrapper] => Array
                                (
                                    [width] => 
                                    [class] => day
                                    [id] => 
                                )

                            [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
                                            [id] => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => am
                                                    [id] => 
                                                )

                                            [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
                                            [id] => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => pm
                                                    [id] => 
                                                )

                                            [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
                                            [id] => 
                                            [class] => 
                                            [conditional_logic] => 0
                                            [parent] => 49
                                            [wrapper] => Array
                                                (
                                                    [width] => 
                                                    [class] => closed
                                                    [id] => 
                                                )

                                            [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
                        )
Share Improve this question asked Feb 1, 2022 at 21:30 Mr.CoderMr.Coder 1112 bronze badges 1
  • having the same issue, but for a repeater field. if I add the output to a debug textarea, it works as expected... I've tried get_field_object(), have_rows(), reset_rows() etc, but as soon as I try to use the values assigned to variables, it breaks both the field group and the display... – Sandra Commented Mar 31, 2022 at 16:29
Add a comment  | 

1 Answer 1

Reset to default 0

I came here looking for an answer to this same error.

I resolved the issue by wrapping my functionality into a function and adding it to the init action.

Here is an example based on above code sample, where get_field() does NOT throw the undefined index: sub_fields... error.

add_action( 'init', 'wpse_402110', 10 );

function wpse_402110(){

  // 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

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269323a1343.html

最新回复(0)