Last active
August 4, 2016 15:45
-
-
Save elimn/d7bc39b59e4f94946a2bb9faad6ce382 to your computer and use it in GitHub Desktop.
MT | TEC | Retrieve the next upcoming recurrence for a given post ID
This file contains 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
<?php | |
/** | |
* Retrieves the next upcoming recurrence for a given post ID | |
* | |
* @param int $event_id The post ID for the event | |
* | |
* @return WP_Post|null The event post object, or null if nothing was found | |
*/ | |
function tribe_get_next_upcoming_recurrence( $event_id ) { | |
if ( ! ( $event = tribe_events_get_event( $event_id ) ) ) return null; | |
$post_status = array( 'publish' ); | |
if ( is_user_logged_in() ) { | |
$post_status[] = 'private'; | |
} | |
$args = array( | |
'start_date' => tribe_format_date( $event->EventStartDate, false, 'Y-m-d H:i:s' ), | |
'post_status' => $post_status, | |
'tribeHideRecurrence' => false, | |
'posts_per_page' => 1, | |
'post__not_in' => array( $event->ID ), // Dont return the current event | |
); | |
// we want event times regardless of whether or not the event is a parent or a child | |
// recurring event. We have to fetch those slightly differently depending on which | |
// it is | |
if ( empty( $event->post_parent ) ) { | |
// we're looking at the master event, so grab the info via the ID | |
$args['p'] = $event->ID; | |
} else { | |
// we're looking at a child event, so grab the info via post_parent | |
$args['post_parent'] = $event->post_parent; | |
} | |
$events = tribe_get_events( $args ); | |
return isset( $events[0] ) ? $events[0] : null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment