Skip to content

Instantly share code, notes, and snippets.

@rochow
Last active October 12, 2020 21:55
Show Gist options
  • Save rochow/b10a9670732f73975882 to your computer and use it in GitHub Desktop.
Save rochow/b10a9670732f73975882 to your computer and use it in GitHub Desktop.
WordPress - Show Only Future Posts/Events
<?php
// Usage: Example below makes the CPT archive "Events" show all upcoming events only, and order by soonest->latest
// Make sure to change the 'key' to your meta_value that has the post ID (you can use future posts but that can cause other problems)
function show_upcoming_events( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_post_type_archive( 'events' )) {
$query->set( 'posts_per_page','-1');
$query->set( 'meta_query', array(
array(
'key' => '_date_event',
'compare' => '>=',
'value' => date('Ymd')
)
));
$query->set( 'meta_key', '_date_event' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'ASC' );
return;
}
}
add_filter( 'pre_get_posts', 'show_upcoming_events' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment