Support

Account

Home Forums ACF PRO Cloned repeater

Solved

Cloned repeater

  • I see a lot of forum posts about clone fields inside repeaters, but I haven’t found any help with getting values from a clone of a repeater. For example, get_sub_field( 'sub_field_name' ) works on the original field, of course, but does not work on the cloned field. ACF treats the cloned field as a sub field of the clone, so get_sub_field( 'repeater_name' ) produces an array of the actual rows I want to process. It’s like I need to do while( have_rows() ) : the_row() inside my main while(have_rows) loop if the field is a clone of a repeater.

    // $area = slug of the field
    while( have_rows( $area, $page_id ) ) : the_row();
        $row = get_row();
        var_dump( $row );
    endwhile;

    Repeater field output:

    array(2) {
      ["field_5a1d86e968289"]=>
      string(10) "search-bar"
      ["field_5a1d95b3d4cc2"]=>
      NULL
    }

    Clone field output: (single row includes multiple rows)

    array(1) {
      ["field_5a1d8827cd34c_field_5a1d7ec7fba73"]=>
      array(2) {
        [0]=>
        array(2) {
          ["field_5a1d86e968289"]=>
          string(14) "featured-books"
          ["field_5a1d95b3d4cc2"]=>
          NULL
        }
        [1]=>
        array(2) {
          ["field_5a1d86e968289"]=>
          string(14) "featured-posts"
          ["field_5a1d95b3d4cc2"]=>
          NULL
        }
      }
    }

    Can I make have_posts() loop over the cloned fields, so they act as normal rows after the_row()?

  • This is the code I ended up with to handle the cloned repeater:

    function my_acf_sections( $area = '' ) {
        if( ! function_exists( 'get_field' ) ) return;
        $page_id = get_queried_object_id();
        if( have_rows( $area, $page_id ) ) :
            while( have_rows( $area, $page_id ) ) : the_row();
                $section_content = get_sub_field( 'section_content' );
                if( $section_content ) {
                    // Do stuff with fields
                } else {
                    // Handle cloned repeater
                    $clone = get_field_object( $area );
                    if( ! isset( $clone['sub_fields'] ) ) continue;
                    $clone = get_field_object( $area )['sub_fields'][0];
                    if( $clone['_clone'] ) my_acf_sections( $clone['name'] );
                }
            endwhile;
        endif;
    }

    I’m open to suggestions for something better.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Cloned repeater’ is closed to new replies.