• I’m having an issue with a WP_Query where I’m trying to show events where either their start date or their finish date are in the future (single day events don’t have finish dates). These events then need to be ordered by their start date.

    But seems that when I use OR in a meta_query the ordering changes. Even if I have the same query related to itself the ordering works with AND but not with OR.

    $query = new WP_Query(
    	array(
    		'post_type' => 'event',
    		'meta_key' => 'start_date',
    		'meta_value' => $todayDate,
    		'meta_compare' => '>=',
    		'orderby' => 'meta_value',
    		'meta_query' => array(
    			'relation' => 'OR',
    			array(
    				'key' => 'start_date',
    				'value' => $todayDate,
    				'compare' => '>='
    			)
    		)
    	)
    );

    Tracing the query it looks like a _edit_last ends up getting included and this is what it ends up getting ordered by.

    Am I doing something wrong here? Or is this a bug that nobody else seems to have stumbled upon?

Viewing 1 replies (of 1 total)
  • I’ve just come across this same issue. I’m glad I am not alone, but sad I have not yet found a solution.

    I suppose we could try applying a filter to ‘fix’ the ordering?

    I’ll update if I find a solution.

Viewing 1 replies (of 1 total)

The topic ‘meta_query OR breaks orderby’ is closed to new replies.