Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible to order my list of custom posts, after filtering it with meta_query, by the meta data of my choice?

For example, I have a custom post type called webinars. I am trying to list all upcoming webinars, and have them ordered by the custom meta field called webinar_startDate.

Using the following query, I was able to return the webinars succesfully excluding the old webinars. However, they still come out in the order they were published, and not by webinar_startDate.

<?php $my_array =  array(
    'meta_query' => array(
            array(
                'key' => 'webinar_startDate',
                'value' => date("Y-m-d H:i:s"),
                'compare' => '>=',
                'type' => 'DATETIME'
            )
    ),
    'orderby' => 'meta_value',
    'post_type' => 'webinars',
    'posts_per_page' => 20,
    'order' => 'ASC'
); ?>

I suspect that due to the change from 3.0 to 3.1, the use of orderby => meta_value is probably different, but I can't find an answer within the WordPress documentation to explain this.

Can anyone help? Thanks in advance.

share|improve this question

2 Answers 2

up vote 14 down vote accepted

the new meta_query array selects which posts the query returns. So yes, you are indicating the 'key' within that meta_query, but you can still use the old method of

'orderby' => 'meta_value', 
'meta_key' => '_events_meta',

in addition to the meta_query, as these lines indicate how to sort the resulting query. So yes, you might indicate the same meta_key twice.

share|improve this answer
    
Awesome, that did the trick. Thanks :) –  Jeff K. Mar 2 '11 at 21:52
7  
This answer is not completely right. If you add an orderby and a meta_key outside of an existing meta_query, the result will indeed sort by the provided key--but it will also then include any post where that meta key is set, even if the value doesn't meet the criteria in the meta_query (at least, that's how it worked for me in testing). A better way to do this might be to pass an array as the orderby param, as described here: core.trac.wordpress.org/ticket/17065#comment:14 –  MathSmath Apr 3 '12 at 16:59

I'm using the following code for my custom posts called events, to get all posts in a Loop.

$evtLoop = new WP_Query(array('post_type' => 'events',
                              'posts_per_page' => 10,
                              'orderby' => 'meta_value', 
                              'meta_key' => '_events_meta',
                              'order'=>'DESC'));

I think you are using your code approximatly the same way. I think you are missing the meta_key with the name of the meta-field to sort. Perhaps it helps if you add

'meta_key' => 'webinar_startDate',

to the outer array?

share|improve this answer
    
I see now. I was confused by the Wordpress documentation that said 'meta_key' and 'meta_value' were depreciated. I assumed that meant I couldn't use them, but I guess that was misleading. Thanks! –  Jeff K. Mar 2 '11 at 21:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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