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)

1 Answer
1

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' => '>='
        )

Leave a Reply

Your email address will not be published. Required fields are marked *