WordPress meta request between two dates

I currently have a small problem with WordPress meta requests. Initial situation:

A custom message type with two meta-fields (offer_start-date, offer_end-date) CPT is intended as a proposal, which should be displayed in a specified period of time (between the start date and end date). The date is formatted in the German DD.MM.YYYY format. To do this, I use the following query:

$args = array(
    'post_type'         => 'offer',
    'posts_per_page'    => -1,
    'post_status'       => 'publish',
    'order'             => 'DESC',
    'meta_query'        => array(
        array(
            'key'       => 'offer_start-date',
            'value'     => date( 'd.m.Y', time() ),
            'type'      => 'numeric',
            'compare'   => '<='
        ),
        array(
            'key'       => 'offer_end-date',
            'value'     => date( 'd.m.Y', time() ),
            'type'      => 'numeric',
            'compare'   => '>='
        )
    )
);
new WP_Query( $args );

Unfortunately, the query does not provide reliable results. I can’t even say 100% why. On some days all offers appear, on other days there are no offers.

I also tried to find out the cause of the problem in Codex, but I seem to be a strong idiot.

+4
2

tu :

'meta_query' => array(
        array(
            'key' => 'event_date',
            'value' => array(date('d/m/Y'), date('d/m/Y', strtotime('28 days'))),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        ),
    )
+1

-

$args = array(
'cat' => $cat_ID,
'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => 'date_from',
        'value'   => date("Y-m-d H:i:s"),
        'compare' => '<=',
        'type'    => 'DATE'
    ),
    array(
        'key'     => 'date_to',
        'value'   => date("Y-m-d H:i:s"),
        'compare' => '>=',
        'type'    => 'DATE'
    )
),
'orderby' => 'date',
'order' => 'DESC'
);
+1

Source: https://habr.com/ru/post/1540711/


All Articles