Skip to content

Instantly share code, notes, and snippets.

@Joeventures
Last active December 28, 2015 23:52
Show Gist options
  • Save Joeventures/77cb16c77328dfb79aab to your computer and use it in GitHub Desktop.
Save Joeventures/77cb16c77328dfb79aab to your computer and use it in GitHub Desktop.
Build Event Array
<?php
// Return an array of course offerings belonging to a category ID
// If it's a multi-week course, it should be set up as a recurring event
// This is a function used in several other areas of the code to simplify
// the process of grabbing event data for C4's class offerings.
function c4_event_array( $catid, $recur = true, $limit = 99 ) {
// First, grab the data based on the criteria given
$post_type = ( true == $recur ? 'event-recurring' : 'event' );
$events = new WP_Query(
array(
'post_type' => $post_type,
'nopaging' => true,
'tax_query' => array( array(
'taxonomy' => 'event-categories',
'field' => 'id',
'terms' => $catid,
)),
'post_status' => 'publish',
'meta_key' => '_event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array( array(
'key' => '_event_start_date',
'value' => date('Y-m-d'),
'compare' => '>=',
'type' => 'DATE'
))
)
);
// Eventually, we'll return an array of the dates and times the
// class will be offered in the future.
$thereturn = '';
$i = 0;
if( $events->have_posts() ) {
while( $events->have_posts() ) {
if ( $limit == $i ) break;
$events->the_post();
$start_date_raw = get_post_meta( get_the_ID(), '_event_start_date', true );
$day_of_week = date( 'l', strtotime( $start_date_raw ));
$day_of_week .= ( true == $recur ? 's, ' : ', ' );
$start_date = date( 'F j', strtotime( $start_date_raw ));
$end_date_raw = get_post_meta( get_the_ID(), '_event_end_date', true );
$end_date = date( 'F j, Y', strtotime( $end_date_raw ));
$start_time_raw = get_post_meta( get_the_ID(), '_event_start_time', true );
$start_time = date( 'g:i a', strtotime( $start_time_raw ));
$end_time_raw = get_post_meta( get_the_ID(), '_event_end_time', true );
$end_time = date( 'g:i a', strtotime( $end_time_raw ));
// Long version
if( true == $recur ) {
$thereturn[$i]['long'] = $day_of_week . $start_date . ' - ' . $end_date . ', ' . $start_time . ' - ' . $end_time;
} else {
$thereturn[$i]['long'] = $day_of_week . $start_date . ', ' . $start_time . ' - ' . $end_time;
}
//Short version
$start_date = date( 'M', strtotime( $start_date_raw ));
$end_date = date( 'M', strtotime( $end_date_raw ));
$year = date( 'Y', strtotime( $start_date_raw ));
$month = ( $start_date == $end_date ? $start_date : $start_date . '/' . $end_date );
$start_time = date( 'g:i', strtotime( $start_time_raw ));
$thereturn[$i]['short'] = $month . ' ' . $start_time . ' ' . $year;
// Is it sold out? 'Yes' if yes.
$sold_out = get_post_meta( get_the_ID(), 'Sold Out', true );
$thereturn[$i]['sold_out'] = $sold_out;
// What is the location?
global $wpdb;
$location_id = get_post_meta( get_the_ID(), '_location_id', true );
$locations_table = EM_LOCATIONS_TABLE;
$location_title = $wpdb->get_row( "SELECT location_name FROM $locations_table WHERE location_id = $location_id", ARRAY_N );
$thereturn[$i]['location'] = $location_title[0];
// What is the Event ID?
$thereturn[$i]['id'] = get_the_ID();
$i++;
}
} else {
$thereturn = null;
}
return $thereturn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment