I have implemented the following code to my them to achieve the custom ordering for featured events (first), regular events (second), and notices (third).
New additions to the code include:
- Merged the posts using
array_merge
like this:$merged_posts = array_merge( $featured_events->posts, $future_events->posts, $notices->posts );
- Ran a
foreach
to set a custom post order for featured events, regular events, and custom post type usingget_post_meta
and$post->post_type === 'tribe_events' )
- Sorted by custom order using
usort( $merged_posts, function ( $a, $b ) { return $a->custom_order <=> $b->custom_order; });
- Used
$merged_post_ids = wp_list_pluck( $merged_posts, 'ID' );
to select the post IDs for the final query. - Added
'post__in' => $merged_post_ids
and'orderby' => 'post__in'
to the final query.
The final working query is below for reference.
// Notices and Events query
$featured_events_args = array(
'post_type' => 'tribe_events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_tribe_featured',
'value' => '1',
'compare' => '=',
),
array(
'key' => '_EventStartDate',
'value' => current_time('Y-m-d H:i:s'),
'compare' => '>=',
'type' => 'DATETIME',
),
),
'orderby' => 'meta_value',
'meta_key' => '_EventStartDate',
'order' => 'ASC',
);
$future_events_args = array(
'post_type' => 'tribe_events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_tribe_featured',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_EventStartDate',
'value' => current_time('Y-m-d H:i:s'),
'compare' => '>=',
'type' => 'DATETIME',
),
),
'orderby' => 'meta_value',
'meta_key' => '_EventStartDate',
'order' => 'ASC',
);
$notices_args = array(
'post_type' => 'notice',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
'taxonomy' => 'notice-type',
'field' => 'term_id',
'terms' => array( 48, 49, 50, 51, 60 ),
),
);
$featured_events = new WP_Query( $featured_events_args );
$future_events = new WP_Query( $future_events_args );
$notices = new WP_Query( $notices_args );
$merged_posts = array_merge( $featured_events->posts, $future_events->posts, $notices->posts );
foreach ( $merged_posts as &$post ) {
if ( get_post_meta( $post->ID, '_tribe_featured', true ) == '1' ) {
$post->custom_order = 0;
} elseif ( $post->post_type === 'tribe_events' ) {
$post->custom_order = 1;
} else {
$post->custom_order = 2;
}
}
// Sort by Custom Order
usort( $merged_posts, function ( $a, $b ) {
return $a->custom_order <=> $b->custom_order;
});
$merged_post_ids = wp_list_pluck( $merged_posts, 'ID' );
$final_query = new WP_Query(
array(
'post_type' => array(
'tribe_events',
'notice'
),
'post__in' => $merged_post_ids,
'posts_per_page' => 4,
'orderby' => 'post__in',
)
);
Thanks to @alexandrucarjan for the assistance, and for leading me toward a solution.