MT - TEC - Redirect Day View to Single Event if there is only 1 event today
<?php
/**
* MT - TEC - Redirect Day View to Single Event if there is only 1 event today
*
* from https://gist.github.com/cliffordp/773c00068499f59adccf76e6b63bcf22
*
* for https://theeventscalendar.com/support/forums/topic/default-view-daily-actual-event/
*/
function cliff_redirect_day_view_single_result_to_event() {
if ( ! function_exists( 'tribe_is_day' ) ) {
return false;
}
global $wp_query;
$is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
if ( 1 !== $wp_query->post_count || ! tribe_is_day() || $is_ajax ) {
return false;
}
$event_id = $wp_query->posts[0];
if ( tribe_is_event( $event_id ) ) {
$current_date = date( 'Y-m-d', current_time( 'timestamp' ) );
$event_date = tribe_get_start_date( $event_id, true, Tribe__Date_Utils:: DBDATEFORMAT );
if ( $current_date === $event_date ) {
$redirect_to = esc_url_raw( get_permalink( $event_id ) );
// 301 is permanent. 302 is temporary.
// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection
// function's default is 302
wp_redirect( $redirect_to );
exit;
}
}
}
add_action( 'template_redirect', 'cliff_redirect_day_view_single_result_to_event' );