Warning: these will be horrible for performance
<?php | |
$date = be_get_date_info(); | |
switch( $date['type'] ) { | |
case 'upcoming': | |
$meta_query = array( | |
array( | |
'key' => 'be_event_start', | |
'value' => current_time('timestamp'), | |
'compare' => '>' | |
) | |
); | |
break; | |
case 'day': | |
$meta_query = array( | |
'relation' => 'OR', | |
/* Single Events */ | |
array( | |
'key' => 'be_event_start', | |
'value' => array( strtotime( date( 'F j, Y', strtotime( $date['date'] ) ) . ' 12:00am' ), strtotime( date( 'F j, Y', strtotime( $date['date'] ) ) . ' 11:59pm' ) ), | |
'compare' => 'BETWEEN', | |
'type' => 'NUMERIC', | |
), | |
/* Multi Day Events */ | |
array( | |
'relation' => 'AND', | |
array( | |
'key' => 'be_event_start', | |
'value' => strtotime( date( 'F j, Y', strtotime( $date['date'] ) ) . ' 12:00am' ), | |
'compare' => '<=', | |
'type' => 'NUMERIC', | |
), | |
array( | |
'key' => 'be_event_end', | |
'value' => strtotime( date( 'F j, Y', strtotime( $date['date'] ) ) . ' 12:00am' ), | |
'compare' => '>=', | |
'type' => 'NUMERIC', | |
) | |
), | |
); | |
break; | |
case 'week': | |
$meta_query = array( | |
array( | |
'key' => 'be_event_start', | |
'value' => array( strtotime( $date['year'] . $date['month'] . $date['start_of_week'] ), strtotime( $date['year'] . $date['month'] . $date['end_of_week'] ) ), | |
'compare' => 'BETWEEN', | |
'type' => 'NUMERIC', | |
) | |
); | |
break; | |
case 'month': | |
$meta_query = array( | |
array( | |
'key' => 'be_event_start', | |
'value' => array( strtotime( $date['year'] . $date['month'] . '01' ), strtotime( $date['year'] . $date['month'] . '31' ) ), | |
'compare' => 'BETWEEN', | |
'type' => 'NUMERIC', | |
) | |
); | |
break; | |
} | |
$tax_query = false; | |
if( isset( $_GET['event_cat'] ) ) | |
$tax_query = array( array( | |
'taxonomy' => 'event-category', | |
'field' => 'slug', | |
'terms' => array( esc_attr( $_GET['event_cat'] ) ), | |
) ); | |
$search = isset( $_GET['event_search'] ) ? esc_html( $_GET['event_search'] ) : false; | |
$loop = new WP_Query( array( | |
'post_type' => 'events', | |
'posts_per_page' => -1, | |
'meta_query' => $meta_query, | |
'tax_query' => $tax_query, | |
's' => $search, | |
'orderby' => 'meta_value_num', | |
'meta_key' => 'be_event_start', | |
'order' => 'ASC', | |
) ); | |
/** | |
* Event Date Info | |
* Creates an array with all relevant information about a day | |
* | |
* @param int $date, yyyymmdd | |
* @return array $output | |
*/ | |
function be_get_date_info( $date = '' ) { | |
global $post; | |
if( empty( $date ) ) { | |
$date = ( isset( $_GET['event_date'] ) && !empty( $_GET['event_date'] ) ) ? (int) $_GET['event_date'] : date( 'Ymd', current_time('timestamp') ); | |
if( is_singular( 'events' ) ) | |
$date = get_post_meta( $post->ID, 'be_event_start', true ); | |
} | |
$output = array(); | |
$output['date'] = $date; | |
$output['time'] = strtotime( $date ); | |
$output['year'] = date( 'Y', $output['time'] ); | |
$output['month'] = date( 'm', $output['time'] ); | |
$output['day'] = date( 'd', $output['time'] ); | |
$output['days_in_month'] = cal_days_in_month( 0, $output['month'], $output['year'] ); | |
$output['day_of_week'] = date( 'D', $output['time'] ); | |
switch($output['day_of_week']){ | |
case "Sun": $output['offset'] = 0; break; | |
case "Mon": $output['offset'] = 1; break; | |
case "Tue": $output['offset'] = 2; break; | |
case "Wed": $output['offset'] = 3; break; | |
case "Thu": $output['offset'] = 4; break; | |
case "Fri": $output['offset'] = 5; break; | |
case "Sat": $output['offset'] = 6; break; | |
} | |
$output['start_of_week'] = date( 'd', $output['time'] ) - $output['offset']; | |
if( $output['start_of_week'] < 1 ) | |
$output['start_of_week'] = 1; | |
$output['start_of_week'] = str_pad( $output['start_of_week'], 2, 0, STR_PAD_LEFT ); | |
$output['end_of_week'] = date( 'd', $output['time'] ) + 6 - $output['offset']; | |
if( $output['end_of_week'] > $output['days_in_month'] ) | |
$output['end_of_week'] = $output['days_in_month']; | |
$output['end_of_week'] = str_pad( $output['end_of_week'], 2, 0, STR_PAD_LEFT ); | |
$prev_month = 1 == $output['month'] ? 12 : $output['month'] - 1; | |
$output['prev_month_date'] = $output['year'] . str_pad( $prev_month, 2, 0, STR_PAD_LEFT ) . '01'; | |
$next_month = 12 == $output['month'] ? 1 : $output['month'] + 1; | |
$output['next_month_date'] = $output['year'] . str_pad( $next_month, 2, 0, STR_PAD_LEFT ) . '01'; | |
$types = array( 'upcoming', 'day', 'week', 'month' ); | |
$output['type'] = ( isset( $_GET['event_type'] ) && !empty( $_GET['event_type'] ) && in_array( $_GET['event_type'], $types ) ) ? $_GET['event_type'] : 'upcoming'; | |
return $output; | |
} |