Home Forums Add-ons Repeater Field Sort by repeater date picker field using if have rows Reply To: Sort by repeater date picker field using if have rows

  • I would loop through all the information and assign it to an array.

      Declare the array outside the loop.
      Add items to the array inside the loop and classify as expired or not.
      Sort the array by expire, date, title, etc.
      Loop through the array and output what you.

      The code below is UNTESTED, but it gives you an idea of how I would go about it.

    //declare array
    $events = array();
    $today = strtotime(date('Ymd'));
    if (have_rows('events')): while (have_rows('events')) : the_row(); 
    //get the expire date
    	$expire = strtotime(get_sub_field('expire_on'));
    //compare it to today and assign it a value
     	if($expire < $today):	
    		$events[]['expired'] = true;
    		$events[]['expired'] = false;
    //assign the other information to key/value in the array
    	$events[]['events_date'] = strtotime(the_sub_field('events_date'));
    	$events[]['events_title'] = the_sub_field('events_title');
        $events[]['events_location'] = the_sub_field('events_location');
    endwhile; endif; 
    //assign some variables for sorting
    foreach ($events as $key => $row) {
        $ev_expire[$key]  = $row['expired'];
        $ev_date[$key] = $row['events_date'];
        $ev_title[$key] = $row['events_title'];
        $ev_location[$key] = $row['events_location'];
    //sorts by 'expired' (true first), then by date (descending), then by title (ascending)
    array_multisort($ev_expire, SORT_DESC, $ev_date, SORT_DESC, $ev_title, SORT_ASC,$events);
    //loop through the array and echo out the info if the event has expired
    foreach ($events as $event){
    		if($event['expired'] == true) {
         		echo "<div class='events-row expired'>";
           		echo "<h2>".$event['events_title']."</h2>";
           		echo "<h3>".date('Ymd',$event['events_date'])."|".$event['events_location']."</h3>