Support

Account

Home Forums Add-ons Repeater Field Add rows to repeater

Solving

Add rows to repeater

  • I’m working through some logic to dynamically build out repeater rows from other acf field groups, that are also repeaters. Getting the fields and sub fields isn’t a problem. I’m just needing some guidance on pushing the values from other field groups to my other field group that’s a repeater (which also has nested repeaters). Here is what I have come up with so far. From my understanding I need to use an array of arrays to make this happen. I can actually get new row to be added, but it doens’t populate with any of the data.

    
    add_action('acf/init', 'bb_populate_boat_fields');
    
    function bb_populate_boat_fields() {
        $boats_repeater = 'field_64b3015ff26f0'; // field key of repeater
    
        $boat_line_rows = array();
        $boat_line_repeater_rows = array(); 
        $boat_line_model_repeater_rows = array();  
    
        while ( have_rows( 'boat_lines', 'option' ) ) : the_row();        
            $line_value = get_sub_field('line_name');
            $line_name = get_sub_field('label');
            $line_image = get_sub_field('image');
            
            $boat_line_repeater_rows[] = array(
                'field_6472614b658a8' => $line_value,
                'field_64726198658a9' => $line_name,
                'field_64b55db947157' => $line_image,
            );         
    
            while( have_rows('model') ) : the_row();
                  // Get sub value.
                  $model_value = get_sub_field('value');
                  $model_name = get_sub_field('name');  
                  $model_image = get_sub_field('image');  
                  
                  $boat_line_model_repeater_rows[] = array(
                    'field_6472622b658aa' => $model_value,
                    'field_64726257658ab' => $model_name,
                    'field_64b498184c717' => $model_image
                  );
    
            endwhile; 
    
            // nested array for every row of repeater
            $boat_line_rows[] = array(
                // field key => value pairs for each sub field            
                'field_64711c7f0ce91' => array(
                    // nested repeater
                    // field key => value pairs for each sub field
                    $boat_line_repeater_rows,
                    'field_647180fd537f2' => array(
                        $boat_line_model_repeater_rows
                    ),
                ),
                // next row in nested repeater
            );
        endwhile;    
    
        //wp_die(var_dump($boat_line_rows));
        
        update_field( $boats_repeater, $boat_line_rows, 'option' );
    }
    
  • You’re code is not easy to follow, but I think that your issue is that you are attempting to use [] to assign values directly to the array as you get them. To build a nested array you need to build the sub array and then add it to the main array all at once.

    Here is a simple example

    
    // initialize the array to hold the repeater value
    $value = array();
    
    // main repeater
    while (have_rows('main_repeater')) {
      the_row();
      // start building this row from sub fields of the main repeater
      $row = array(
        // add fields of the main repeater
        'field_key' => get_sub_field('field_name')
      );
      // sub repeater
      if (have_rows('sub_repeater')) {
        // initialize the sub repeater array
        $sub_repeater = array();
        while(have_rows('sub_repeater')) {
          the_row();
          // add a row to sub repeater array
          $sub_repeater[] = array(
            // add fields from sub repeater
            'field_key' => get_sub_field('field_name')
          );
        } // end sub repeater while
        // add sub repeater to the current row
        $row['sub_field_repeater_key'] = $sub_repeater;
      } // end sub repeater if
      // add the new row to the main repeater
      $value[] = $row;
    } // end main repeater while
    
  • Thanks John, I copied in your example but it still doesn’t seem to be working quite right. I can create correct number of row, but they are empty. Maybe a screenshot would help show what I have?

  • Here is what the logic I have produces:

  • John, I actually got this working now.

    // start building this row from sub fields of the main repeater
        while ( have_rows( 'boat_lines', 'option' ) ) : the_row();        
            $line_value = get_sub_field('line_name');
            $line_name = get_sub_field('label');
            $line_image = get_sub_field('image');
            
            $boat_line_repeater_rows = array(
                // add fields of the main repeater
                'field_64dfd8bf4bc3f' => $line_value,
                'field_64dfd8bf4bc40' => $line_name,
                'field_64dfd8bf4bc41' => $line_image,
            );   
    
            // sub repeater        
            if ( have_rows('model') ) {
                // initialize the sub repeater array
                $boat_line_model_repeater_rows = array();
    
                while( have_rows('model') ) : the_row();
                    // Get sub value.
                    $model_value = get_sub_field('value');
                    $model_name = get_sub_field('name');  
                    $model_image = get_sub_field('image');  
                    
                    // add a row to sub repeater array
                    $boat_line_model_repeater_rows[] = array(
                        // add fields from sub repeater
                        'field_64dfd8bf4bc43' => $model_value,
                        'field_64dfd8bf4bc44' => $model_name,
                        'field_64dfd8bf4bc45' => $model_image
                    );
    
                endwhile; // end sub repeater while
    
                // add sub repeater to the current row
                $boat_line_repeater_rows['field_64dfd8bf4bc42'] = $boat_line_model_repeater_rows;
            } // end sub repeater if
    
            // add the new row to the main repeater
            $boat_line_rows[] = array(
                // field key => value pairs for each sub field            
                'field_64dfd8bf4bc3e' => array(
                    $boat_line_repeater_rows
                )
                // next row in nested repeater
            );
    
        endwhile; // end main repeater while 
  • So here is what the current logic produces:

    array(3) {
        [
            0
        ]=> array(1) {
            [
                "field_64dfd8bf4bc3e"
            ]=> array(4) {
                [
                    "field_64dfd8bf4bc3f"
                ]=> string(8) "kencraft" [
                    "field_64dfd8bf4bc40"
                ]=> string(8) "Kencraft" [
                    "field_64dfd8bf4bc41"
                ]=> string(69) "http://kencraftboats.wpdev.me/wp-content/uploads/2023/07/kencraft.jpg" [
                    "field_64dfd8bf4bc42"
                ]=> array(2) {
                    [
                        0
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(13) "21cc_walkdown" [
                            "field_64dfd8bf4bc44"
                        ]=> string(13) "21CC WALKDOWN" [
                            "field_64dfd8bf4bc45"
                        ]=> string(74) "http://kencraftboats.wpdev.me/wp-content/uploads/2023/07/21cc_walkdown.jpg"
                    } [
                        1
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(13) "25cc_walkdown" [
                            "field_64dfd8bf4bc44"
                        ]=> string(13) "25CC WALKDOWN" [
                            "field_64dfd8bf4bc45"
                        ]=> string(74) "http://kencraftboats.wpdev.me/wp-content/uploads/2023/07/25cc_walkdown.jpg"
                    }
                }
            }
        } [
            1
        ]=> array(1) {
            [
                "field_64dfd8bf4bc3e"
            ]=> array(4) {
                [
                    "field_64dfd8bf4bc3f"
                ]=> string(19) "bayrider_flatbottom" [
                    "field_64dfd8bf4bc40"
                ]=> string(20) "Bay Rider Flatbottom" [
                    "field_64dfd8bf4bc41"
                ]=> string(80) "http://kencraftboats.wpdev.me/wp-content/uploads/2023/07/bayrider_flatbottom.jpg" [
                    "field_64dfd8bf4bc42"
                ]=> array(5) {
                    [
                        0
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(14) "bay_rider_2460" [
                            "field_64dfd8bf4bc44"
                        ]=> string(14) "BAY RIDER 2460" [
                            "field_64dfd8bf4bc45"
                        ]=> string(75) "http://kencraftboats.wpdev.me/wp-content/uploads/2023/08/bay_rider_2460.jpg"
                    } [
                        1
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(16) "bay_rider_2260fs" [
                            "field_64dfd8bf4bc44"
                        ]=> string(16) "BAY RIDER 2260fs" [
                            "field_64dfd8bf4bc45"
                        ]=> bool(false)
                    } [
                        2
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(14) "bay_rider_2260" [
                            "field_64dfd8bf4bc44"
                        ]=> string(14) "BAY RIDER 2260" [
                            "field_64dfd8bf4bc45"
                        ]=> bool(false)
                    } [
                        3
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(14) "bay_rider_2260" [
                            "field_64dfd8bf4bc44"
                        ]=> string(14) "BAY RIDER 2260" [
                            "field_64dfd8bf4bc45"
                        ]=> bool(false)
                    } [
                        4
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(14) "bay_rider_2060" [
                            "field_64dfd8bf4bc44"
                        ]=> string(14) "BAY RIDER 2060" [
                            "field_64dfd8bf4bc45"
                        ]=> bool(false)
                    }
                }
            }
        } [
            2
        ]=> array(1) {
            [
                "field_64dfd8bf4bc3e"
            ]=> array(4) {
                [
                    "field_64dfd8bf4bc3f"
                ]=> string(12) "bayrider_bay" [
                    "field_64dfd8bf4bc40"
                ]=> string(13) "Bay Rider Bay" [
                    "field_64dfd8bf4bc41"
                ]=> string(73) "http://kencraftboats.wpdev.me/wp-content/uploads/2023/07/bayrider_bay.jpg" [
                    "field_64dfd8bf4bc42"
                ]=> array(2) {
                    [
                        0
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(17) "bay_rider_219_bay" [
                            "field_64dfd8bf4bc44"
                        ]=> string(17) "BAY RIDER 219 BAY" [
                            "field_64dfd8bf4bc45"
                        ]=> string(78) "http://kencraftboats.wpdev.me/wp-content/uploads/2023/08/bay_rider_219_bay.jpg"
                    } [
                        1
                    ]=> array(3) {
                        [
                            "field_64dfd8bf4bc43"
                        ]=> string(17) "bay_rider_239_bay" [
                            "field_64dfd8bf4bc44"
                        ]=> string(17) "BAY RIDER 239 BAY" [
                            "field_64dfd8bf4bc45"
                        ]=> string(78) "http://kencraftboats.wpdev.me/wp-content/uploads/2023/08/bay_rider_239_bay.jpg"
                    }
                }
            }
        }
    }
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.