2

My Query is this:

$products = new WP_Query( array(
        'post_type'      => 'ys_product',
        'orderby'        => array( 'date' ),
        'order'          => 'DESC',
        'posts_per_page' => 8,
        'meta_query'     => array(
            'relation' => 'AND',
            array(
                'key'   => 'ys_product_status',
                'value' => 'ok'
            ),
            array (
                'key'   => 'ys_product_start',
                'value' => date('Ymd'),
                'compare' => '>='
            ),
            array (
                'key'   => 'ys_product_end',
                'value' => date('Ymd'),
                'compare' => '<='
            )
        )
    ) );

And on my DB I have two items with post_type 'ys_product', and with post_meta like this:

Item 1

  • meta_key ys_product_status = 'ok'
  • meta_key ys_product_start = '20141101'
  • meta_key ys_product_end = '20141230'

Item 2

  • meta_key ys_product_status = 'ok'
  • meta_key ys_product_start = '20141101'
  • meta_key ys_product_end = '20150131'

The result of date('Ymd') today is '20141226', which seems clearly between those boundaries.

But if I search only filtering by 'ys_product_status' == 'ok'; I get my two items. But when I add my two other meta, it wont return any results at all.

What am I doing wrong?

Thanks and regards. (Using WP 4.1)

5
  • Recently someone else had a similar issue and it was solved using 'type' => 'DATE' in the meta query for fields with dates. See wordpress.stackexchange.com/questions/173100/…
    – cybmeta
    Commented Dec 26, 2014 at 16:50
  • No, it can work with chars as well. Problem is a typo, just realized... :( My comparison signs are flipped.
    – user47133
    Commented Dec 26, 2014 at 17:03
  • Yes it can, but it won't be consistent. But think to change the data format to MySQL date format or UNIX date format. For future it can be better, scalable, etc.
    – cybmeta
    Commented Dec 26, 2014 at 17:10
  • On any case, it's not related to the problem or the question. Thanks anyway.
    – user47133
    Commented Dec 26, 2014 at 17:23
  • On any case I posted it as a comment and not as an answer trying to help you in making things better ;). It is up to you if you accept the suggestion or not.
    – cybmeta
    Commented Dec 26, 2014 at 17:25

1 Answer 1

2

Stupid typo I'm afraid.

This:

array (
            'key'   => 'ys_product_start',
            'value' => date('Ymd'),
            'compare' => '>='
        ),
        array (
            'key'   => 'ys_product_end',
            'value' => date('Ymd'),
            'compare' => '<='
        )

Should be like this:

array (
            'key'   => 'ys_product_start',
            'value' => date('Ymd'),
            'compare' => '<='
        ),
        array (
            'key'   => 'ys_product_end',
            'value' => date('Ymd'),
            'compare' => '>='
        )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.