0

I am trying to orderby my CPT if the current time is in between two meta fields. (i.e, the current_time() is between two time fields.).

meta_query returns only the posts that are between the values, but not all posts. Here is what I have tried.

    $args['meta_query'] = array(
        array(
            'key' => 'open_today',
            'value' => strtolower(current_time('l')),
        ),
        array(
            'key' => 'open_time',
            'value' => current_time(),
            'compare' => '>='
        ),
        array(
            'key' => 'close_time',
            'value' => current_time(),
            'compare' => '<'
        ),
    );

Basically what I am trying to achieve is that sort restaurants by open time. So, when a user selects to sort the listing by Open Now, the query returns the resturants open now at first and then rest of them.

  • 1
    Is that compre just a typo in the question? Because it should be compare.. – Sally CJ Mar 3 at 9:14
  • I would create 2 queries, one with all the open restaurants, and create an array variable to store all the IDs (call this $exclude_open), and then a second query with the rest of posts using 'post__not_in' => $exclude_open. – Dragos Micu Mar 3 at 9:51
  • @SallyCJ Yes, its just a typo. Sorry, fixed. – Abhik Mar 3 at 10:13
  • Okay, but there's another typo there - current_time() requires the first parameter. And in your actual $args, did you set the type parameter? Because you should, unless if the value is text/char. So if the value is like 07:00 (a time), then you'd use 'type' => 'TIME'; or if it's a timestamp, you can use NUMERIC instead. – Sally CJ Mar 4 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.