Last active
January 18, 2016 21:34
-
-
Save tammyhart/7c79aea5688eed9e6c6a to your computer and use it in GitHub Desktop.
A pre_get_posts action that will first skip past events, then order them by the date meta value ascending
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
/** | |
* Order Events by date and skip them completely if they are past | |
*/ | |
function coh_event_post_order( $query ) { | |
// do not modify queries in the admin | |
if( is_admin() ) { | |
return $query; | |
} | |
// only modify queries for 'event' post type | |
if( isset( $query->query_vars['post_type'] ) && $query->query_vars['post_type'] == 'event' ) { | |
// skip past events | |
$meta_query = array( | |
'relation' => 'AND', | |
array( | |
'key' => 'date', | |
'value' => date( 'Ymd' ), | |
'type' => 'numeric', | |
'compare' => '>', | |
) | |
); | |
$query->set('meta_query', $meta_query ); | |
// reorder | |
$query->set( 'meta_key', 'date' ); | |
$query->set( 'orderby', 'meta_value' ); | |
$query->set( 'order', 'ASC' ); | |
} | |
// return | |
return $query; | |
} | |
add_action( 'pre_get_posts', 'coh_event_post_order' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment