0

The first query is working fine individually. Vouchers have a day, a start time and an end time. I'm grabbing the current time (e.g 11:10) and trying to show posts where the $time_now comes BETWEEN the start_time and end_time. Are you not able to pass an array into the key?

$today = date('l');
$time_now = date('H:i');

    $args = array (
        'post_type'              => array( 'voucher' ),
        'meta_query'             => array(
            array(
                'key'       => '_voucher_days_available',
                'value'     => $today,
                'compare'   => 'LIKE',
                'type'      => 'CHAR',
            ),
            array(
                'key' => array('_voucher_start_time','_voucher_end_time'),
                'value' => $time_now,
                'compare' => 'BETWEEN',
                ),
        ),
    );
  • What kind of value does _voucher_start_time hold? is it numeric, timestamp, or something else? – Ahmed Fouad Sep 28 '16 at 10:46
  • @AhmedMahdi It's saved simply as a string "14:00" using github.com/WebDevStudios/CMB2/wiki/Field-Types#text_time – Joe Turner Sep 28 '16 at 10:53
  • So just to recap $time_now is somethin like 16:00 start time is 14:00 and end time is saved as 18:00 for example. Is that correct please? – Ahmed Fouad Sep 28 '16 at 10:55
  • @AhmedMahdi Correct. I'm looking to get all vouchers inside that time frame. – Joe Turner Sep 28 '16 at 10:57
2

In addition to the suggestion by @AhmedMahdi to solve your problem, here's the narrower answer to your question: Nope, key cannot be an array. As you can see in the specs of WP_Meta_Query key must be a string.

1

Can you please try the following $args

$today = date('l');
$time_now = date('H:i');

$args = array (
    'post_type'              => array( 'voucher' ),
    'meta_query'             => array(
            'relation'      => 'AND',
            array(
                'key'       => '_voucher_days_available',
                'value'     => $today,
                'compare'   => 'LIKE',
                'type'      => 'CHAR',
            ),
            array(
                'key'       => '_voucher_start_time',
                'value'     => $time_now,
                'compare'   => '>=',
                'type'      => 'date'
            ),
            array(
                'key'       => '_voucher_end_time',
                'value'     => $time_now,
                'compare'   => '<=',
                'type'      => 'date'
            ),
    ),
);
  • Thanks for the answer but this is still returning 0 results despite the criteria being met in the database. – Joe Turner Sep 28 '16 at 13:51
  • Ah, I know. Both 'compare' should be '>=' – Joe Turner Sep 28 '16 at 14:41
  • Actually no, that's still not done the trick. – Joe Turner Sep 28 '16 at 15:18
  • What this query do is ensure that time now is between (ranges) of voucher start time and voucher end time, please ensure that the data is set / received as a date. – Ahmed Fouad Sep 28 '16 at 15:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.