Tribe__Events__Main::get_closest_event( WP_Post $post, string $mode = 'next' )
Get the prev/next post for a given event. Ordered by start date instead of ID.
Contents
Parameters
- $post
-
(WP_Post) (Required) The post/event.
- $mode
-
(string) (Optional) Either 'next' or 'previous'.
Default value: 'next'
Return
(null|WP_Post)
Source
File: src/Tribe/Main.php
public function get_closest_event( $post, $mode = 'next' ) {
if ( 'previous' === $mode ) {
$order = 'DESC';
$direction = '<=';
$this->_where_direction = '<';
} else {
$order = 'ASC';
$direction = '>=';
$this->_where_direction = '>';
$mode = 'next';
}
// This property and the `_where_x` ones are temporary by design to get around 5.2 lack of closures.
$this->_where_compare = $direction;
$this->_where_post_id = $post->ID;
$start_date = get_post_meta( $post->ID, '_EventStartDate', true );
$this->_where_start_date = $start_date;
$args = array(
'post__not_in' => array( $post->ID ),
'meta_key' => '_EventStartDate',
'orderby' => array( '_EventStartDate' => $order, 'ID' => $order ),
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => '_EventStartDate',
'value' => $start_date,
'compare' => $direction,
),
array(
'key' => '_EventHideFromUpcoming',
'compare' => 'NOT EXISTS',
),
'relation' => 'AND',
),
);
/**
* Allows the query arguments used when retrieving the next/previous event link
* to be modified.
*
* @var array $args
* @var WP_Post $post
*/
$args = (array) apply_filters( "tribe_events_get_{$mode}_event_link", $args, $post );
add_filter( 'posts_where', array( $this, 'get_closest_event_where' ), 10, 2 );
$results = tribe_get_events( $args );
remove_filter( 'posts_where', array( $this, 'get_closest_event_where' ) );
// Unset the temporary properties we set to get around lack of closure support in PHP 5.2.
unset( $this->_where_direction, $this->_where_compare, $this->_where_post_id, $this->_where_start_date );
$event = null;
// If we successfully located the next/prev event, we should have precisely one element in $results
if ( 1 === count( $results ) ) {
$event = current( $results );
}
/**
* Affords an opportunity to modify the event used to generate the event link (typically for
* the next or previous event in relation to $post).
*
* @var WP_Post $post
* @var string $mode (typically "previous" or "next")
*/
return apply_filters( 'tribe_events_get_closest_event', $event, $post, $mode );
}