Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Created May 12, 2025 10:14
Show Gist options
  • Save andrasguseo/f30ed161c5da047e5397de5f96052740 to your computer and use it in GitHub Desktop.
Save andrasguseo/f30ed161c5da047e5397de5f96052740 to your computer and use it in GitHub Desktop.
TEC > Next and previous links on the single event page point to the next event in the event's category
<?php
/**
* Next and previous links on the single event page point to the next event in the event's category.
* If an event has no categories, it will show the next event, regardless of any categories.
* If an event has multiple categories, it will show the next event which has any of the categories.
* After that, it will show events having categories of this event.
*
* Usage: Add the snippet with Code Snippets or to your functions.php file.
*
* @author: Andras Guseo
*
* Plugins required: The Events Calendar
*
* @since May 12, 2025 Initial version.
*/
add_filter( 'tribe_events_get_closest_event', function ( $event, $post_obj, $mode ) {
global $post;
// Get the categories of the event.
$categories = tribe_get_event_cat_ids( $post->ID );
// Bail if the event has no categories, and show the next event.
// NOTE, if the next event has a category, then only events in that category will be shown next.
if ( empty( $categories ) ) {
return $event;
}
if ( $event ) {
$is_next = false;
foreach ( $categories as $index => $category_slug ) {
// If the next/prev event is in one of the categories, set it as next.
if ( tribe_event_in_category ( $category_slug, $event->ID ) ) {
$is_next = true;
}
}
// If there is no next event set then look for the next.
if ( ! $is_next ) {
$find_next_closest_event = new Tribe__Events__Adjacent_Events();
$find_next_closest_event->set_current_event_id ($event->ID);
return $find_next_closest_event->get_closest_event($mode);
}
}
return $event;
}, 100, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment