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.

this is my problem query, which picks all future or pending events.
Note that some posts only have _startdate without _enddate (1-day events).

$args = array( 
'post_type'=> 'events', 
'meta_query' => array(
    array(
    'key' => '_startdate',
    'value' => $today,
    'type' => 'DATE',
    'compare' => '>='
    ),
    array(
    'key' => '_enddate',
    'type' => 'DATE',
    'value' => $today,
    'compare' => '>='
    ),
    'relation' => 'OR',
    ),
'meta_key'=> '_city', 
'orderby' => 'meta_value', 
'order' => 'ASC',
) ;

Everything works great until I change _city to _startdate or _enddate.

Can you please help me with this query (to sort by _startdate ) ? Thanks

share|improve this question
    
I'm having the same problem. Seems that you cannot sort by the same metakeys that are being compared in a relationship? –  Ray Gulick Feb 7 '12 at 4:47
    
Actually, it does not seem that sorting with the SAME metakey names is the problem. Just tried adding another custom field to create another start date with a different metakey name (2 start dates by different names) and that does not allow sorting either. Seems I can either do the combined query OR sort, but not both. –  Ray Gulick Feb 7 '12 at 5:01
add comment

2 Answers

You could try

$args = array(
    'post_type'=> 'events',
    'meta_query' => array(
        array(
            'key' => '_startdate',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',
        ),
        array(
            'key' => '_enddate',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',
        ),
        'relation' => 'OR',
    ),
);
function jumpin_thru_hoops( $a ) {
    global $wpdb;
    $a = $wpdb->postmeta.'.meta_value+0 ASC';
    return $a;
}
add_filter( 'posts_orderby', 'jumpin_thru_hoops' );
$upcoming = new WP_Query( $args );
// or
$upcoming = get_posts( $args );
remove_filter( 'posts_orderby', 'jumpin_thru_hoops' );
share|improve this answer
add comment

Replace 'meta_key'=> '_city' with 'meta_key'=> '_startdate'.

share|improve this answer
    
That's the problem: when you change _city to _startdate, it reverts to sorting by post date. –  Ray Gulick Feb 7 '12 at 4:48
    
Are you sure post_date doesn't have the same value as the _startdate? –  scribu Feb 7 '12 at 17:52
    
I'm sure it is not the same value. –  Ray Gulick Feb 7 '12 at 21:28
    
I've tried to report this same bug, but couldn't explain it well enough –  WraithKenny Oct 25 '12 at 22:54
    
@WraithKenny Link? –  scribu Oct 28 '12 at 14:03
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.