Support

Account

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

  • 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') ): ?>