Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Meta-Query between 2 dates

I have currently a small problem understanding with meta-queries by WordPress. Initial situation:

A Custom Post Type with 2 meta-Fields (offer_start-date, offer_end-date) The CPT is intended as an offer, which should be displayed in the specified time period (between start-date and end-date) . The date here is formatted in german format DD.MM.YYYY. For that I use currently 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 yield reliable results. I can not even say 100% why. On some days all offers appear, on other days there are no offers.

I have also tried to find out the cause of the problem in the Codex, but it seems I am a strong blockhead.

like image 693
mitopp Avatar asked May 15 '14 13:05

mitopp


People also ask

What is WordPress meta query and how to use it?

As you can see, the WordPress meta query is quite powerful and you can use it to customize your WordPress site if you know how to use it. Don’t be afraid to experiment with it and you’ll master it in no time.

How to query only posts with ID of 1 in WordPress?

In order to query only the posts that we want to show on the page with ID of 1, we can use the following query: As you can see. we need to use the ‘meta_key’ parameter to tell the WP_Query which meta field to query by, and the ‘meta_value’ parameter is used to provide ID of the page that we want the posts to be shown on.

What is the ‘Meta_type’ parameter in WordPress?

As you can see, we’ve used the ‘meta_type’ parameter to tell WordPress that the value needs to be sorted as a numeric value. Other data types that ‘meta_type’ supports are ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’. Since WordPress 4.2, you can order posts by multiple meta values.

How to use meta Query option in SQL Server?

One more handy way to use a meta query option is to combine the conditions in a meta query. You can use binary “OR” relation as well as the “AND” relation. Here is how it works:


2 Answers

If you need tu use between just use like that:

'meta_query' => array(
        array(
            'key' => 'event_date',
            'value' => array(date('d/m/Y'), date('d/m/Y', strtotime('28 days'))),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        ),
    )
like image 132
Otto Avatar answered Sep 30 '22 02:09

Otto


This below solution may be handy to someone

$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'
);
like image 32
Grigoriy Avatar answered Sep 30 '22 03:09

Grigoriy