Support

Account

Home Forums Bug Reports Bug(?): flexible content & nested repeater fields

Solved

Bug(?): flexible content & nested repeater fields

  • Hello,

    With repeater fields I can get the row data (array) and number of rows by running:

    $array = get_field('repeater_field_name'); 
    $row_count = count($array);

    but when I try the same in a repeater field nested inside a flexible content block
    $array = get_field('nested_repeater_field_name');

    I’m using it like this:

    if( have_rows('carousel') ){
     $rows = get_field('carousel');
     $row_count = count($rows);
     <!-- do some count based stuff here -->
     while( have_rows('carousel') ): the_row();
     endwhile;
    }

    When in a flexible content field the repeater $rows returns null, unless I’m missing something (which is entirely possible). I can get the row count by iterating through the rows but I would have expected consistent behaviour. Is this a bug or expected behaviour, if it’s the latter is there an alternative way (without iterating) of getting the row count?

    Many thanks

    —- Versions —-
    ACF v5.3.6.1
    WP v4.4.2

  • Inside a flexible field you need to use get_sub_field().

  • Hello John,

    You pointed me in the right direction – turns out it also cannot be called after has_rows(), so the solution looks like this:

    
    $rows = get_field('carousel');
    $row_count = count($rows);
    if( have_rows('carousel') ){
     <!-- do some count based stuff here -->
     while( have_rows('carousel') ): the_row();
     <!-- do some other stuff here -->
     endwhile;
    }
  • repeaters inside of flex fields work the same as nested repeaters. A flex field is basically a repeater with some additional properties. There is a document on nested repeaters https://www.advancedcustomfields.com/resources/working-with-nested-repeaters/

  • Hello John,

    Thanks for your time and responses, as this isn’t really a bug I’ll mark your last response as a solution.

    Is there any reason that a normal nested repeater can call get_field within has_rows() but get_sub_field has to be called outside of has_rows() for a repeater inside a flexible content field?

    Many thanks

  • Your question actually has me a little lost.

    You can use get_field() for a top level repeater field, or any top level field.

    get_field() should always return top level fields, not sub fields of any repeater or flex field. Using get_field() inside a repeater loop will not return values from the rows of a repeater. get_sub_field() shouldn’t get anything outside of the current repeater loop. If you are using get_field() inside a repeater then you’re probably not seeing what you think your seeing.

    Here is some code illustrating a three level nesting of repeater type fields with comments about what will be returned where.

    When I use get_field() or get_sub_field() this also refers to their equivalent function the_field() and the_sub_field().

    
    // repeater loop
    // this could be a flex field or a repeater
    // both are treated the same as the loop is concerned
    if (have_rows('flex_field')) {
      while (have_rows('flex_field')) {
        the_row();
        /* 
           using get_field() here will not 
           return values from rows of the flex field
           get_field() will still return values from 
           top level fields
           you must use get_sub_field() to get values 
           from the flex field row
        */
        
        // repeater in the flex field
        if (have_rows('repeater')) {
          while (have_rows('repeater')) {
            the_row();
            /* using get_field() here will still only 
               return top level fields not not value or
               the flex field or this repeater
               you must use get_sub_field()
               it is impossible to get other values from 
               the flex field until after the end of this 
               loop get_sub_field() will return sub fields 
               of this repeater only
            */
            
            // nested repeater
            if (have_rows('nested_repeater')) {
              while (have_rows('nested_repeater')) {
                the_row();
                /* 
                   using get_field() here will still only 
                   return top level fields and not values of 
                   the flex field or the first repeater
                   you must use get_sub_field()
                   get_sub_field() will only return values 
                   from rows of this nested repeater
                */
              } // end while have_rows nested repeater
            } // end if have_rows nested repeater
          } // end while have_rows for repeater
        } // end if have_rows repeater
      } // end while have_rows for flex field
    } // end if have_rows flex_field
    
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Bug(?): flexible content & nested repeater fields’ is closed to new replies.