Last active
October 12, 2020 21:55
-
-
Save rochow/b10a9670732f73975882 to your computer and use it in GitHub Desktop.
WordPress - Show Only Future Posts/Events
This file contains hidden or 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
<?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