1

I am using Advanced custom fields with separate date and time fields for events. What I need to do is show all the events that haven't yet occurred.

I have managed to show the events in order by date so far but what I need to do now is now have each one in order by the time they will occur also. Once the time has occurred the event needs to be removed from the list.

My args for the events list so far looks like this...

$today = date('Ymd');
$time = date('H:i:s');
$compCount = array(
  'post_type' => 'product',
  'posts_per_page'  => -1,
  'meta_query' => array(
    array(
      'key'     => 'comp_closing',
      'compare' => '>=',
      'value'       => $today,
    )
  ),
  'orderby'   => 'meta_value_num',
  'order'     => 'ASC',
);

my field for the date is comp_closing and the time is closing_time.

I have tried using relation with 2 different meta arrays but have found it confusing to get anything to work correctly.

Marijan
  • 1,825
  • 1
  • 13
  • 18
  • _“I have managed to show the events in order by date”_ - that must be rather by accident then, I suppose - because the documentation clearly states, that when using `'orderby' => 'meta_value_num'`, a `'meta_key' => 'keyname'` _must_ also be present, which you are currently missing. – CBroe Mar 09 '20 at 13:02
  • _“my field for the date is `comp_closing` and the time is `closing_time`”_ - then you will have to check whether the closing date is today _and_ the closing time is later than now, _or_ the date itself is still in the future. – CBroe Mar 09 '20 at 13:04
  • @CBroe Thank you for your insight, if you could explain to me how this is done that would be very helpful. – Mark Feltwell Mar 09 '20 at 13:07

1 Answers1

1

Although I don’t know how the fields comp_closing and closing_time are stored in the database this may point you in the right direction.

$today = date('Y-m-d');
$time = date('H:i:s');

$compCount = array(
  'post_type' => 'product',
  'posts_per_page'  => -1,
  'meta_query' => array(
    'relation' => 'OR',
    // make sure the date is after the current date...
    array(
      'key'     => 'comp_closing',
      'compare' => '>',
      'value'       => $today,
    ),
    // ...or if the date is the same...
    array(
      'relation' => 'AND',
      array(
        'key'     => 'comp_closing',
        'compare' => '=',
        'value'       => $today,
      ),
      // ...make sure we didn’t hit the time yet.
      array(
        'key'     => 'closing_time',
        'compare' => '>',
        'value'       => $time,
      )
    )
  ),
  'orderby'   => 'meta_value_num',
  'order'     => 'ASC',
);
Marijan
  • 1,825
  • 1
  • 13
  • 18
  • Thank you! That does make sense to me and works! The only thing I need to get right now is to make sure that the events are sorted by Time also if i have more than one event on the same day. So the event ending first is at the top of the list. – Mark Feltwell Mar 09 '20 at 13:26
  • [This blog](https://rudrastyh.com/wordpress/meta_query.html#order_multiple_keys) post may help you on how to order by meta keys. I wonder if you could ease the pain by just using a single *datetime* field for the date. Please accept the answer if it did help you. – Marijan Mar 09 '20 at 13:30