Support

Account

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

Solving

Sort by repeater date picker field using if have rows

  • I have a repeater called events.
    I use a date picker field called expire_on to hide the events if they have passed the expiration date.

    I would like to sort the events by the expiration date. Can someone help me modify my below code to do that? Thanks!

    <?php if (have_rows('events')): while (have_rows('events')) : the_row(); ?>
    
            <?php
            date_default_timezone_set('America/Los_Angeles');
            $today = date('Ymd');
            $expire = get_sub_field('expire_on');
    
            ?>
            <div class="events-row <?php if ($expire < $today) { ?>expired<?php } ?>">
              <h2><?php the_sub_field('events_title'); ?></h2>
              <h3><?php the_sub_field('events_date'); ?> | <?php the_sub_field('events_location'); ?></h3>
             </div>
    <?php endwhile; endif; ?>
  • 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.

    <?php
    
    //declare array
    $events = array();
    date_default_timezone_set('America/Los_Angeles');
    
    $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;
    	else:
    		$events[]['expired'] = false;
    	endif;	
    
    //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>
             </div>";
     		}
    
    }
    
    ?>
  • Thank you. I wrote it as an array and it works. I guess the array is necessary in order to sort the data before displaying it.

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

The topic ‘Sort by repeater date picker field using if have rows’ is closed to new replies.