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.

I have a custom query and I am filtering out events by custom field: event-month. I simple filter our the events that dont have a month_number = to date("n");

Its a pretty great little query, but I need to orderby another custom field, event_date.

Do I need a custom function or something to get this done. I simply want to orderby => event_date

but I am currently using event-month for my query.

<?php 
        $event_query = new WP_Query(
        array( 
          'post_type'   => 'event',        // only query events
          'meta_key'    => 'event-month',  // load up the event_date meta
          'order_by'    => '',
          'order'       => 'asc',         // ascending, so earlier events first
          'meta_query'  => array(
             array(         // restrict posts based on meta values
              'key'     => 'event-month',  // which meta to query
              'value'   => date("n"),  // value for comparison
              'compare' => '=',          // method of comparison
              'type'    => 'NUMERIC'         // datatype, we don't want to compare the string values
            ) // meta_query is an array of query ites
           ) // end meta_query array
          ) // end array
        ); // close WP_Query constructor call

?>

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

Despite meta_key being deprecated, it is still required to get the orderby to work correctly.

First and foremost though, there's an error with your code, and that's in using order_by and not orderby (there's no underscore in the orderby arg).

Give this a shot and see how it works out for you.

$event_query = new WP_Query( array( 
    'post_type'   => 'event',
    'meta_key'    => 'event-month',
    'meta_query'  => array(
        array( 
            'key'     => 'event-month',
            'value'   => date("n"),
            'compare' => '=',
            'type'    => 'NUMERIC'
        )
    ),
    'orderby'    => 'meta_value',
    'order'       => 'asc', 
) );

If you want to add a second meta key, and sort by that key, just ensure that key is inside the meta_key arg, eg.

$event_query = new WP_Query( array( 
    'post_type'   => 'event',
    'meta_key'    => 'some-key',
    'meta_query'  => array(
        array( 
            'key'     => 'some-key',
            'value'   => 'whatever',
            'compare' => '=',
            'type'    => 'NUMERIC'
        ),
        array( 
            'key'     => 'event-month',
            'value'   => date("n"),
            'compare' => '=',
            'type'    => 'NUMERIC'
        )
    ),
    'orderby'    => 'meta_value',
    'order'       => 'asc', 
) );

Odd that you should need meta_key for the sort, but i don't see the orderby being respected without it, i can see how the query appears inside debug bar's queries tab and as far as i can tell meta_key is currently required to get a proper sort on meta_value.

share|improve this answer
    
t31os, this looks right. I am not 100% sure hot to move forward but this is pointing me in the right direction. Is there any connection between the initial meta_key/value and the meta_key/value in the meta_query? OR is it essentially saying, find all by these params then compare against the meta_query? If the later is the case then I could simply put event_date in the initial meta_key and then handle the event-month compare in the meta query. Does that make sense? –  tjsherrill Mar 8 '11 at 16:14
    
The standalone meta_key is purely for the sort order, meta_query does all of the work though, ie. figuring out which posts match the given criteria. Removing the meta_key arg would just result in you losing the meta_value sort order. –  t31os Mar 8 '11 at 16:32
    
t31os, thanks for the help. I have it working just fine. –  tjsherrill Mar 8 '11 at 17:17
    
This answer was just incredibly helpful to me! The codex does not explain this at all and because meta_key was marked as deprecated, I was avoiding it like the plague. –  tollmanz May 7 '11 at 20:59
    
Just to add something to this gr8 answer - for those who wish to sort by a numeric value replace 'orderby' => 'meta_value' to 'orderby' => 'meta_value_num' –  Sagive SEO Jun 4 '13 at 19:04
add comment

Remove 'meta_key' from params. It doesn't work in WP 3.1

share|improve this answer
    
Thanks for the note: but any ideas on how to order the results by a second key/value? –  tjsherrill Mar 8 '11 at 6:01
    
Not true, it's just a deprecated parameter, it still functions as intended however. You will also need the parameter to get a proper sort, despite the deprecation(unless that bug has now been rectified in core). –  t31os Jan 28 at 15:56
add comment

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.