Support

Account

Forum Replies Created

  • Ok, so I’ve solved this but not by using meta_query but by sorting the array of events after the query.

    I added a new entry to my events array called “datetime”.

        if( get_field('all_day_event', $event_item->ID) != true ):
    
          $events[$i]['datetime'] = date_format($start_date, "Y-m-d") . ' ' . get_field('start_time', $event_item->ID);
    
          $events[$i]['time'] = get_field('start_time', $event_item->ID) . ( get_field('end_time', $event_item->ID) ? ' - ' . get_field('end_time', $event_item->ID) : '' );
    
        else:
    
          $events[$i]['datetime'] = date_format($start_date, "Y-m-d") . ' 0:00';
    
          $events[$i]['time'] = null;
    
        endif;

    I then sorted the events array by using the below.

      $compare_function = function($a,$b) {
        $a_timestamp = strtotime($a['datetime']); // convert a (string) date/time to a (int) timestamp
        $b_timestamp = strtotime($b['datetime']);
            // new feature in php 7
        return $a_timestamp <=> $b_timestamp;
      };
    
      usort($events, $compare_function);
  • I can see in a forum post below that using clauses might be the way forward but as you can see from the below example

    'meta_query' => array(
    	'relation' => 'AND',
        'date_clause' => array(
            'key' => 'event_date',
    		'compare'	=> '=',
        ),
        'time_clause' => array(
            'key' => 'event_time',
    		'compare'	=> '=',
        ),
    ),
    'orderby' => array(
    	'date_clause' => 'ASC',
    	'time_clause' => 'ASC',
    ),
    

    Which I’ve borrowed from that post is that my needs include an OR relation for start and end date (end date like start and end time are also optional) so can a clause include an OR relation inside of it or is there a different way to solve this?

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