Skip to content

Instantly share code, notes, and snippets.

@nitishn
Created June 10, 2016 22:07
Show Gist options
  • Save nitishn/ed0355185cc1ee5ca79345d3b6bd92b0 to your computer and use it in GitHub Desktop.
Save nitishn/ed0355185cc1ee5ca79345d3b6bd92b0 to your computer and use it in GitHub Desktop.
/**
* Grab the upcoming events from the Events Manager plugin. However, we need to make sure only the first
* instance of a recurring event shows up. So, we need to essentially get reccuring/non-recurring events
* and then merge the result set. This is not supported by the plugin currently...
* @return array
*/
function getUpcomingEvents()
{
$upcomingEvents = array();
$recurring = array();
$nonRecurring = array();
if( class_exists('EM_Events') ) {
// Grab the recurring and non recurring events
$recurring = \EM_Events::get(array('orderby' => 'event_start_date', 'recurring' => 1));
$nonRecurring = \EM_Events::get(array('orderby' => 'event_start_date', 'recurring' => 0));
// Filter out recurring events from the non recurring set
$nonRecurring = array_filter( $nonRecurring, function($event) { return $event->recurrence_id == null; } );
// Merge the two sets of events
$upcomingEvents = array_merge( $recurring, $nonRecurring );
// Sort the events in order
usort( $upcomingEvents, function($curEvent, $nxtEvent) { return $curEvent->start > $nxtEvent->start; } );
}
// only return 3 events
return array_splice($upcomingEvents, 0, 3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment