Home › Forums › Add-ons › Repeater Field › 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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.