0

This question already has an answer here:

I've tried doing a WP_Query based on a custom field, which name is 'date' and value is something like '14/01/2015'. I want the query to give results when 'date' is between today and tomorrow.

All I've tried so far is this:

$y=date('d/m/Y',strtotime('yesterday'));
$t=date('d/m/Y',strtotime('tomorrow'));
$o=date('d/m/Y',strtotime('today'));

echo "yesterday: ".$y;
echo "today: ".$o;
echo "tomorrow: ".$t;
$d=new WP_Query(
    array(
        'category__in' => array(4),
        'post_type' => 'post',
        'posts_per_page' => '-1',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'meta_query' => array(
            'key' => 'data',
            'value'     => array($y,$t),
            'compare'   => 'BETWEEN',
            'type'      => 'DATE',
        )
    )
);

But is giving me posts which 'date' is also 25/01/2015. What am I doing wrong?
Thanks in advice

marked as duplicate by cybmeta, Nicolai, kaiser Jan 15 '15 at 2:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Your date format is backwards, though it may sometimes appear to work, you will get incorrect results in many cases. – Milo Jan 14 '15 at 14:48
0

Problem solved,
I've used this:

$d=new WP_Query(
    array(
        'category__in' => array(4),
        'post_type' => 'post',
        'posts_per_page' => '-1',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'data',
                'value' => $t,
                'compare' => '<='
            ),
            array(
                'key' => 'data',
                'value' => $o,
                'compare' => '>='
            )
        ),
    )
);

Instead of the other one before, and everything is working fine.

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