Created
July 6, 2023 12:07
-
-
Save mikeoberdick/654d371774eac246a28a6f9d11bfe9c0 to your computer and use it in GitHub Desktop.
Filter the query on the events archive to only show upcoming events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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