Skip to content

Instantly share code, notes, and snippets.

@tammyhart
Last active January 18, 2016 21:34
Show Gist options
  • Save tammyhart/7c79aea5688eed9e6c6a to your computer and use it in GitHub Desktop.
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
/**
* 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