0

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 .

  • You haven't made this easy with the date format you're storing. Is there any option on the cards to change this to a numeric format e.g. dmY? – TheDeadMedic Apr 20 '16 at 10:58
  • yes, i can change the date format when storing . Because it is my custom query . – Manik Apr 21 '16 at 6:35
  • could you please help to solve this question stackoverflow.com/questions/36526268/… – Manik Apr 21 '16 at 6:36
1

I think you need to do something like this:

<?php
$args = array(
    'post_type'         => 'shop',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'meta_query'        => array(
        'relation'      => 'AND',
        array(
            'key'       => 'cu_status',
            'value'     => array( 'pending', 'processing' ),
            'compare'   => 'IN',
        ),
        array(
            'key'       => 'cu_date',
            'value'     => array( '2016-01-12', '2016-01-13' ),
            'compare'   => 'BETWEEN',
            'type'      => 'DATE'
        )
    )
);
?>
  • 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 . – Manik Apr 17 '16 at 14:00
  • But it is not correct also – Manik Apr 17 '16 at 14:01
  • I store cu_date in (31-02-2016) format . Please help . – Manik Apr 17 '16 at 14:01
  • I get the it . Your code is working . i Changed the date format into 2016-01-13 – Manik Apr 17 '16 at 14:13
  • Could you please see this problem stackoverflow.com/questions/36595547/… – Manik Apr 17 '16 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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