1

I have a wp_query that gets all custom post types in ascending order from today, to list some events on a site of mine. While this works fine:

 $args = array( 'post_type'         =>  'events',
                                'posts_per_page'    =>  5,
                                'orderby'           =>  'meta_value', 
                                'order'             =>  'ASC',
                                'meta_key'          =>  'my_special_date',
                                'meta_value'        =>  date('Ymd',strtotime("today")),
                                'meta_compare'      =>  '>=' ); 

I am not getting the same results when I convert this to using meta_query, like so:

array(  'post_type'         =>  'events',
                'posts_per_page'    =>  5,
                'orderby'           =>  'meta_value', 
                'order'             =>  'ASC',
                'meta_query'        =>  array(
                                                array(
                                                        'key'           => 'my_special_date',
                                                        'value'         => date('Ymd',strtotime("today")),
                                                        'compare'       => '>=',
                                                        'type'          => 'NUMERIC'
                                                        )

                                            )

                 ); 

Note that I'm not comparing dates, I'm comparing numbers, as the date is in the Ymd format. the meta key is in this format. I am having some rather odd results from this - I can't quite work out what's wrong. Anything look obviously wrong to any of you?

2

Firstly, there's no point in converting to 'meta_query' if you're dealing with a single custom field.

Secondly, 'meta_query' doesn't handle ordering.

Finally, since your date is in the 'Ymd' format, comparing them as numbers will produce incorrect results. In your initial code, they are compared as strings.

  • Hey scribu, thanks for answering. I know it's pointless with a single custom field. I have a few things to compare, this is all part of an ajax system that brings posts through based on certain options picked by people, hence the change. I've removed the type=numeric, and the compare is still not working. Any idea why? Thanks! – Michael Watson Sep 13 '11 at 21:16
  • Scrap that scribu - I've worked out what you were saying. Ordering needed to be with meta_key just like the olden days, is all. (and scrapping the numeric as you mentioned). Thanks (and thanks for your writing on it in the first place on your blog!) – Michael Watson Sep 13 '11 at 21:21
1

Simple solution:

orderby => meta_key still needs to be set as meta_query does not handle ordering.

Also, I was comparing char data as numeric.

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.