Support

Account

Home Forums General Issues Sorting Date Range Issue

Solved

Sorting Date Range Issue

  • Hi all,
    I’m having issues sorting our events by dates. Currently it’s sorting by publish date and not the event date. Here’s what we have so far.

    We have two fields for events. A field for events that just have a single date and another field for events that are multiple days. (At the time we set up the site it was my understanding that ACF didn’t have a date range built into the date picker so we set it up with if statements on the front end)

    We’ve been using this bit of code:

    $i = 4; 
    global $post;
     
     $args = array( 
    'numberposts'   => -1, 
    'post_type'     => 'events', 
    'orderby'       => 'date', 
    'order'         => 'ASC', 
    'meta_query' => array(
    array(
    	'key' => 'display_on_upcoming_events',
    	'value' => '1',
    	'compare' => '=='
    	)
     ); 
    $myposts = get_posts($args);
    
    				    if($myposts):
    
    				      $chunks = array_chunk($myposts, $i);
    				      $html = "";
    
    				      foreach($chunks as $chunk):
    				        ($chunk === reset($chunks)) ? $active = "active" : $active = "";
    				       	echo '<div class="item '. $active .'"><ul class="events-list">';
    
    				        foreach($chunk as $post):
    
    				          if( get_field( 'date_range' )) { 
    				          	echo '<li><span class="date">' . get_field( 'event_start_date' ) . ' ' . get_field( 'event_end_date' ) . '</span>'; 
    				          } else if( get_field( 'event_date' )) { 
    				          	echo '<li><span class="date">' . get_field( 'event_date' ) . '</span>';
    				          }
    
    				          echo '<a href="';
    				          echo the_permalink();
    				          echo '" title="Event: ' . get_the_title( $post->ID ) . '">';
    				          echo get_the_title( $post->ID ) . '</a></li>';
    
    				        endforeach;
    
    				        echo '</ul></div>';  
    
    				      endforeach;
    
    				      echo $html;
    
    				    endif;
    

    And it works fine, fine meaning will display on the upcoming events area.

    What I’m looking to do is sort the events by start date, either single day event or using the date range not by published date.

    Can anyone point me in the right direction? Thanks

  • I’m not sure if this will work with get_posts(), you may need to switch to WP_Query https://codex.wordpress.org/Class_Reference/WP_Query, if you do you’ll need to change numberposts to posts_per_page

    
    $args = array( 
      'numberposts'   => -1,
      'post_type'     => 'events', 
      'meta_query' => array(
        array(
          'key' => 'display_on_upcoming_events',
          'value' => '1',
          'compare' => '=='
        ),
        'date_clause' => array(
          'key' => 'event_start_date',
          'compare' => 'EXISTS'
        ),
      ),
      'orderby'       => array('date_clause' => 'ASC'), 
    );
    
  • Thanks John! I adjusted the code a bit and it works great. Here’s what worked for me in case anyone else has this issue.

    $args = array( 
    	'posts_per_page'   => -1,
    	'post_type'     => 'events',
    	'order' => 'ASC', 
    	'meta_query' => array(
    		array(
    			'key' => 'display_on_upcoming_events',
    			'value' => '1',
    			'compare' => '=='
    		),
    		 'date_clause' => array(
    			'key' => 'event_date',
    		'compare' => 'EXISTS'
    					    ),
    					  ),
    					  'orderby' => 'meta_value_num',
    					  'meta_key' => 'event_date', 
    					);
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Sorting Date Range Issue’ is closed to new replies.