Home › Forums › Add-ons › Repeater Field › Nested Repeater: Sort Order
Hi there,
I’ve got a repeater field ‘jc’ with a sub-repeater ‘attachments’.
I’d like to sort JC rows with the newest ones displayed at the top (so, in a descending order).
I’ve tried to adapt this (https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/) to my code below, but have been unable to. By that I mean it would print fine, but it didn’t change the order, whether I put sort_asc or sort_desc, didn’t make a difference.
This is my code that’s working perfectly except there’s no sorting.
<?php
// check for JC rows (parent repeater)
if( have_rows('jc') ):
?>
<div class="su-accordion">
<?php
// loop through rows (parent repeater)
while( have_rows('jc') ): the_row(); ?>
<div class="su-spoiler su-spoiler-style-simple su-spoiler-icon-plus su-spoiler-closed">
<div class="su-spoiler-title">
<?php the_sub_field('month_year'); ?>
</div><!-- / spoiler title -->
<?php
// check for ATTACHMENT rows (sub repeater)
if( have_rows('attachments') ): ?>
<div class="su-spoiler-content su-clearfix">
<?php
// loop through rows (sub repeater)
while( have_rows('attachments') ): the_row();
$file = get_sub_field('file');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
?>
<p>
<a href="<?php echo $url; ?>" target="_blank">
<?php echo $title; ?>
</a>
</p>
<?php endif; ?>
<?php endwhile; ?>
</div><!-- / spoiler content -->
<?php endif; //if( get_sub_field('month_year') ): ?>
</div>
<?php endwhile; // while( has_sub_field('jc') ): ?>
</div>
<?php endif; // if( get_field('jc') ): ?>
I’ve made some progress – the sort now works but since there’s a ‘foreach’, the entire content is repeated. I have 8 sortable rows with subcontent right now, and everything is printed 8 times with the code below (but now sorted correctly in a descending order):
<?php
// check for rows (parent repeater)
if( have_rows('jc') ):
// get repeater field data
$jc = get_field('jc');
// vars
$order = array();
// populate order
foreach( $jc as $i => $row ) {
$order[ $i ] = $row['id'];
}
// multisort
array_multisort( $order, SORT_DESC, $jc );
// loop through repeater
if( $jc ):
?>
<div class="su-accordion">
<?php
// loop through rows (parent repeater)
while( have_rows('jc') ): the_row(); ?>
<?php foreach( $jc as $i => $row ): ?>
<div class="su-spoiler su-spoiler-style-simple su-spoiler-icon-plus su-spoiler-closed">
<div class="su-spoiler-title">
<?php echo $row['id']; ?><?php echo $row['month_year']; ?>
</div><!-- / spoiler title -->
<?php
// check for rows (sub repeater)
if( have_rows('attachments') ): ?>
<div class="su-spoiler-content su-clearfix">
<?php
// loop through rows (sub repeater)
while( have_rows('attachments') ): the_row();
$file = get_sub_field('file');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
?>
<p>
<a href="<?php echo $url; ?>" target="_blank">
<?php echo $title; ?>
</a>
</p>
<?php endif; ?>
<?php endwhile; ?>
</div><!-- / spoiler content -->
<?php endif; //if( get_sub_field('month_year') ): ?>
</div>
<?php endforeach; ?>
<?php endwhile; // while( has_sub_field('jc') ): ?>
</div>
<?php endif; // if( get_field('jc') ): ?>
<?php endif; // if( get_field('jc') ): ?>
You will need to get the entire repeater and do your sorting and then loop though the rows of the repeater using the array rather than using an ACF have_rows() loops.
$repeater = get_field('js');
then loop though the array and do your sorting, when that is done loop through the array again and output what you want to output.
The topic ‘Nested Repeater: Sort Order’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.