1

I'm trying do order a custom post type page by 2 variables: - first is the custom post type meta_key "date" - then, for posts with the same date, it should be ordered by meta_key "number"

I'm willing to set for some kind of ordering on the second level but right now it really seems random:

 <?php $my_query = new WP_Query( array( 
                              'post_type' => 'product', 
                              'meta_query' => array(
                                          array(
                                                  'key' => 'options_type',
                                                  'value' => 'Denim'
                                                  ),
                                      ),
                               'orderby'  => 'meta_value',
                               'meta_key' => 'options_date',
                               'posts_per_page' => 50 ,
                               'order'    => 'DESC'


                              )); ?>

Even editing the "wordpress publication date" doesn't affect the "same day" order. So I end up with:

  • 09.03.2012 244 Diesel (wordpress date 3.8.2012)
  • 09.03.2012 246 Levi's (wordpress date 3.9.2012)
  • 09.03.2012 247 Evisu (wordpress date 3.10.2012)

(i'm using european date displayng, so 09.03 means March, 9th)


edit

just to clarify: I've some custom field and i've set a specific options_date field for the custom post type, which is different from the wordpress date.

I would like to order the posts using a multiple order: custom field "options_date" and, inside the same "options_date" by another custom field "options_info".

ordering with

'orderby'  => 'meta_value date',

doesn't seem to work and it's not the target result, I'm looking for a multiple meta_key ordering.

8
  • Try to add both meta values that you want to orderby into the value and separate them with a space.
    – kaiser
    Commented Mar 12, 2012 at 16:03
  • with 'meta_key' => 'options_date options_number', I get nothing in return. the site is not broken, but no posts appear.
    – molokom
    Commented Mar 12, 2012 at 16:15
  • 1
    Please re-read my comment. I said if you want to orderby 2 values, then you'll have to add both to orderby. So 'orderby' => 'date meta_value' would be what I suggested.
    – kaiser
    Commented Mar 12, 2012 at 16:19
  • Ups, sorry. Almost there... Now inside the same "option_date" the order is correct (wordpress date), but the ASC/DESC seems to control just the second ordering, the first on is always ASC (older option_date post apper first).
    – molokom
    Commented Mar 12, 2012 at 16:26
  • Update your question with the output from this answers code.
    – kaiser
    Commented Mar 12, 2012 at 17:01

0

Your Answer

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

Browse other questions tagged or ask your own question.