I would like to change the sort order of a query, based on meta in the returned posts. Specifically, posts are events with an event date and I filter them as either future or past. If all events are in the past, I want the most recent ones first, if all in the future, the next to happen needs to be first. I've tried all sorts in a pre-get-posts function like:
function resort($query){
if($query->is_main_query() && is_tax('event-category'){
if($query->have_posts()){
// do something based on the first post
wp_reset_query();
}
}
}
The function is in functions.php and is firing but it doesn't get past have_posts()
, even though the main query is returning lots of posts. What am I missing?
- What is it that triggers the query, and can't it be triggered
anywhere? I tried
query_posts()
and failed. - How can I construct a custom query which is exactly the main query?
- expense doesn't bother me - it's a small site.