1

Hi in my post_type=shop i have 2 meta keys and array of values

Custom fields

 Name           Values

 cu_status   pending,processing,completed

 cu_date     12-Jan-2016 , 13-Jan-2016, ......  any date in the same format date("d-M-Y")

Now i need to loop through all posts with cu_status =pending,processing and cu_date is between 12-Jan-2016 to 13-Apr-2016 What will the query ?

Iam very confused . For to get all post with status pending,processing I know the query

 $args = array(

        'post_type'         => 'shop',

        'post_status'       => 'publish',
        'meta_query' => array(
       array(
           'key' => 'cu_status',
           'value' => array('pending','processing'),
           'compare' => 'IN',
           )
           ),
           'posts_per_page' => -1
                );

Pleases help to complete the query .

1 Answer 1

2

You need to use the relation operator e.g.

      $args = array(
        'post_type'         => 'shop',
        'post_status'       => 'publish',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'cu_status',
                'value' => array('pending','processing'),
                'compare' => 'IN',
                ) , 
            array(
                'key' => 'cu_date',
                'value' => array($start, $end),
                'compare' => 'BETWEEN',
                'type' => 'DATE'
                )   
            ),
        'posts_per_page' => -1
        );

Also use the compare=>BETWEEN for getting the difference in 2 dates. You may need to tweak the code a bit as I have not tested it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you . but for me it is not working . 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'cu_status', 'value' => array( 'pending', 'processing' ), 'compare' => 'IN', ), array( 'relation' => 'AND', array( 'key' => 'cu_date', 'value' => '04-02-2016', 'compare' => '>=', ), array( 'key' => 'cu_date', 'value' => '04-03-2016', 'compare' => '<=', )) ) This is working . But it is not correct also . .
Your code is working . i Changed the date format into 2016-01-13
No Problem. Glad I could help :)
could you please help to solve this question stackoverflow.com/questions/36526268/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.