Support

Account

Home Forums Add-ons Repeater Field Sort Nested Repeater Fields Using Sub Fields Reply To: Sort Nested Repeater Fields Using Sub Fields

  • Hi @smspaul

    Thanks again for your work today on the support forum. I just read over your question again, and think I can help a bit more.

    Using the get_field_objects will get you all your repeater fields, this is good and perhaps I oversaw the usefulness.

    I can see what you are doing wiht your setup and I would like to say firstly, that the flexible content field will do all this for you and probably save you some dev time!

    That aside, to sort the data, you will need to merge all the rows together into 1 array like so, and then sort them:

    
    <?php 
    
    $rows = array();
    $fields = get_field_objects();
    
    if( $fields )
    {
    	foreach( $fields as $field )
    	{
    		$field_label = $field['label'];
    		$field_name = $field['name'];
    		
    		// loop through repeater rows
    		if( $field['value'] )
    		{
    			foreach( $field['value'] as $row )
    			{
    				// add extra data to row
    				$row['field_label'] = $field_label;
    				$row['field_name'] = $field_name;
    				
    				// append row
    				$rows[] = $row;
    			}
    		}
    	}
    }	
    
    // test all rows of data
    echo '<pre>';
    	print_r($rows);
    echo '</pre>';
    die;	
    
    // sort rows
    $order_by = array();
    foreach( $rows as $i => $row ) {
    	$order_by[ $i ] = $row['order'];
    }
    						
    array_multisort( $order_by, SORT_ASC, $rows );
    
    // test sorted rows
    echo '<pre>';
    	print_r($rows);
    echo '</pre>';
    die;
    
    ?>
    

    Hope that helps.

    Cheers
    E