Skip to content

Instantly share code, notes, and snippets.

@ohryan
Last active December 27, 2023 17:00
Show Gist options
  • Save ohryan/e23a8694423283c335965dc8b932f229 to your computer and use it in GitHub Desktop.
Save ohryan/e23a8694423283c335965dc8b932f229 to your computer and use it in GitHub Desktop.
The Event Calendar - Query Loop
<?php
add_filter( 'query_loop_block_query_vars', 'tec_order_by_date' );
function tec_order_by_date( $query ) {
// ignore if the query block is not using this post type
if ( 'tribe_events' !== $query['post_type'] ) {
return $query;
}
// always exclude events with dates in the past
$query['meta_key'] = '_EventStartDate';
$query['meta_value'] = date( 'Y-m-d' );
$query['meta_compare'] = '>=';
// If date order was chosen in the block settings, change to use the Event date instead of Post date
if ( 'date' === $query['orderby'] ) {
$query['orderby'] = '_EventStartDate';
$query['order'] = 'ASC';
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment