Support

Account

Home Forums Add-ons Repeater Field Nested Repeater: Sort Order

Solving

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.

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

You must be logged in to reply to this topic.