WP Query to display events and posts at the same page

This 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();
?>

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)