Skip to content

Instantly share code, notes, and snippets.

@christianwach
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianwach/90d636ac51e0af43096b to your computer and use it in GitHub Desktop.
Save christianwach/90d636ac51e0af43096b to your computer and use it in GitHub Desktop.
Upcoming Events for Event Organiser
/**
* Amend query for upcoming events
*/
function bpeo_upcoming_events_page( $query ) {
// only run when EO active on main site
if ( function_exists( 'eventorganiser_is_event_query' ) ) {
if ( eventorganiser_is_event_query( $query ) ) {
if ( $query->get( 'bpeo' ) AND 'upcoming-events' == $query->get( 'bpeo' ) ) {
// show only upcoming events
$query->set( 'event_start_after', 'now' );
// this is needed for now, but maybe redundant in the future...
$query->set( 'showpastevents', true );
}
}
}
}
add_action( 'pre_get_posts', 'bpeo_upcoming_events_page', 5 );
/**
* Amend query vars for upcoming events (see above)
*/
function bpeo_register_query_vars( $qvars ){
// only run when EO active on main site
if ( function_exists( 'eventorganiser_is_event_query' ) ) {
$qvars[] = 'bpeo';
}
// --<
return $qvars;
}
add_filter( 'query_vars', 'bpeo_register_query_vars' );
/**
* Amend rewrite rules for upcoming events (see above)
*/
function bpeo_add_rewrite_rule() {
// only run when EO active on main site
if ( ! function_exists( 'eventorganiser_is_event_query' ) ) return;
global $wp_rewrite;
// get base regex
$regex = str_replace( '%event%', 'upcoming-events', $wp_rewrite->get_extra_permastruct( 'event' ) );
// get pagination base regex
$pageregex = $wp_rewrite->pagination_base . '/?([0-9]{1,})/?$';
// add paged rewrite rule
add_rewrite_rule( $regex . '/' . $pageregex, 'index.php?post_type=event&paged=$matches[1]&event_start_after=now&bpeo=upcoming-events', 'top' );
// add standard rewrite url
add_rewrite_rule( $regex, 'index.php?post_type=event&event_start_after=now&bpeo=upcoming-events', 'top' );
}
add_action( 'init', 'bpeo_add_rewrite_rule', 11 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment