Support

Account

Home Forums Add-ons Repeater Field Reverse the Repeater Rows

Solved

Reverse the Repeater Rows

  • Hi,
    I am trying to reverse sort the repeater rows – currently I can display my repeater rows using

    function trs_testimonial_loop () {
    
    //My ACF Fields for reference
    //testimonials - field group
    //testimonial - sub-field
    //testimonial_header - sub-field
    
    // check if the repeater field has rows of data
    if( have_rows('testimonials') ):
    
     	// loop through the rows of data
        while ( have_rows('testimonials') ) : the_row();
    
            // display a sub field value
    		echo '<article><div class="entry-content testimonial">';
    		echo '<p>' . get_sub_field('testimonial') . '</p>';
    		echo '<h2>' . get_sub_field('testimonial_header') . '</h2>';
    		echo '</div></article>';
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;
    		
    }

    But if I try to use the code example for sorting like this…

    function trs_testimonial_loop_sort() {
    
    //My ACF Fields for reference
    //testimonials - field group
    //testimonial - sub-field
    //testimonial_header - sub-field
    
    $repeater = get_field('testimonials');
    
    /*
    *  Obtain a list of columns
    *  - $column_id will look something like this:
    
    Array
    (
        [0] => 3
        [1] => 2
        [2] => 4
        [3] => 1
    )
    
    */
    
    foreach( $repeater as $key => $row ) {
        
        $column_id[ $key ] = $row['testimonial'];
    }
    
    /*
    *  Use the $column_id array to sort the $repeater array
    */
    
    array_multisort( $column_id, SORT_ASC, $repeater );
    
    /*
    *  Do stuff with the repeater... Note: has_sub_field loop will not use the "sorted" $repeater array
    */
    
    	foreach( $repeater as $row ) {
    		 // display a sub field value
    		echo '<article><div class="entry-content testimonial">';
    		echo '<p>' . get_sub_field('testimonial') . '</p>';
    		echo '<h2>' . get_sub_field('testimonial_header') . '</h2>';
    		echo '</div></article>';
    		}
    }

    I get no output at all – I know I am missing something fundamental – any advice

  • would this work? :

    foreach( $repeater as $row ) {
    $testimonial_p = $row['testimonial'];
    $testimonial_h2 = $row['testimonial_header'];
    
    echo '<article><div class="entry-content testimonial">';
    echo '<p>' . $testimonial_p . '</p>';
    echo '<h2>' . $testimonial_h2 . '</h2>';
    echo '</div></article>';
    }
  • thank you very much that now outputs and sorts – I do have a follow up question about sorting by date entered – I’ll post a new thread.

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

The topic ‘Reverse the Repeater Rows’ is closed to new replies.