Skip to content

Instantly share code, notes, and snippets.

@mikeoberdick
Created July 6, 2023 12:07
Show Gist options
  • Save mikeoberdick/654d371774eac246a28a6f9d11bfe9c0 to your computer and use it in GitHub Desktop.
Save mikeoberdick/654d371774eac246a28a6f9d11bfe9c0 to your computer and use it in GitHub Desktop.
Filter the query on the events archive to only show upcoming events
function show_upcoming_events( $query ) {
if( is_admin() ) {
return $query;
}
if ( ( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'event' ) ) {
$today = date('Y-m-d H:i:s', strtotime("-1 days"));
$meta_query = array(
array(
'key' => 'date',
'compare' => '>',
'value' => $today,
'type' => 'DATETIME'
)
);
$query->set('meta_query', $meta_query);
$query->set('order', 'ASC');
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'date');
$query->set('meta_type', 'DATETIME');
};
// return
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment