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 .

Manik
  • 513
  • 1
  • 7
  • 23

1 Answers1

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.

Richard Miles
  • 557
  • 4
  • 18
  • 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 . . – Manik Apr 17 '16 at 14:02
  • Your code is working . i Changed the date format into 2016-01-13 – Manik Apr 17 '16 at 14:13
  • No Problem. Glad I could help :) – Richard Miles Apr 17 '16 at 14:27
  • could you please help to solve this question http://stackoverflow.com/questions/36526268/sql-query-to-download-order-report-in-woocommerce – Manik Apr 10 '17 at 08:54