I am trying to sort events (from event-organiser plugin) and posts in a page where posts would be ordered by date and events ordered by eventstart date with a WP_Query by selecting the appropriate categories (1 for posts and 621 for events).
$my_query = new WP_Query(array(
'post_type'=> array ('post', 'event'),
'post_status'=>'publish',
'category__in' => array (1, 621),
'orderby' => array(
'eventstart' => 'DESC',
'date' => 'DESC'
),
'posts_per_page'=>-1
));
This works fine except the events are sorted by published date (as the posts).
With the documentation, I've tried multiple things by adding:
'events_start_after' => 'today',
'event_series' => eo_get_the_start(),
'group_events_by' => 'occurrence',
Doesn't change anything.
Any idea how to solve this?
Thx in advance.
I am trying to sort events (from event-organiser plugin) and posts in a page where posts would be ordered by date and events ordered by eventstart date with a WP_Query by selecting the appropriate categories (1 for posts and 621 for events).
$my_query = new WP_Query(array(
'post_type'=> array ('post', 'event'),
'post_status'=>'publish',
'category__in' => array (1, 621),
'orderby' => array(
'eventstart' => 'DESC',
'date' => 'DESC'
),
'posts_per_page'=>-1
));
This works fine except the events are sorted by published date (as the posts).
With the documentation, I've tried multiple things by adding:
'events_start_after' => 'today',
'event_series' => eo_get_the_start(),
'group_events_by' => 'occurrence',
Doesn't change anything.
Any idea how to solve this?
Thx in advance.
Share Improve this question asked Apr 2 at 17:26 made leodmade leod 856 bronze badges 1 |1 Answer
Reset to default 0This is a pretty common issue when you're mixing regular posts with custom post types like "event" (from the Event Organiser plugin). The main problem is that WP_Query
defaults to sorting by post_date
, so it doesn’t know to use a custom field like eventstart.
One way around this is to fetch the posts separately and then manually sort them by the field you want — that usually gives you better control.
But if you really want to use a combined WP_Query
, it’s still possible — it just takes a bit of tweaking to get it right.
<?php
// Get posts
$posts = get_posts(array(
'post_type' => 'post',
'category__in' => array(1),
'post_status' => 'publish',
'posts_per_page' => -1,
));
// Get events
$events = get_posts(array(
'post_type' => 'event',
'category__in' => array(621),
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => '_event_start_date', // Event Organiser stores it like this
'orderby' => 'meta_value',
'order' => 'DESC',
));
// Add a custom key to sort both by a unified 'date' value
foreach ($posts as &$post) {
$post->sort_date = get_the_date('Y-m-d H:i:s', $post);
}
foreach ($events as &$event) {
$event->sort_date = get_post_meta($event->ID, '_event_start_date', true);
}
// Merge and sort
$merged = array_merge($posts, $events);
usort($merged, function($a, $b) {
return strtotime($b->sort_date) - strtotime($a->sort_date);
});
// Output
foreach ($merged as $item) {
setup_postdata($item);
echo '<h2>' . get_the_title($item) . '</h2>';
echo '<p>' . $item->sort_date . '</p>';
}
wp_reset_postdata();
?>
event
post type may not use thecategory
taxonomy (I'm not familiar with the plugin). You may want to consider a Tax_Query instead ofcategory__in
in which you can do arelation => 'OR'
– Howdy_McGee ♦ Commented Apr 2 at 17:38