Skip to content

Instantly share code, notes, and snippets.

@kevincolten
Created August 4, 2018 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincolten/387163c1b7e7e1d03f41b4907b71f27b to your computer and use it in GitHub Desktop.
Save kevincolten/387163c1b7e7e1d03f41b4907b71f27b to your computer and use it in GitHub Desktop.
Return all events
function GetUpcomingEvents() {
$events = array();
$index = 0;
$today = date( "m/d/Y" );
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => - 1,
'meta_key' => 'event_start_date',
'orderby' => 'event_start_date',
'order' => 'ASC',
// 'meta_query' => array(
// array(
// 'key' => 'event_end_date',
// 'compare' => '>=',
// 'value' => $today,
// //'type' => 'numeric'
// )
// )
);
// $posts_args = get_posts( $args );
// echo '<pre>' . print_r( $posts_args, true ) . '</pre>';
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$event_id = $query->post->ID;
$events[ $index ][ 'event_id' ] = $event_id;
$events[ $index ][ 'event_title' ] = $query->post->post_title;
$events[ $index ][ 'event_content' ] = $query->post->post_content; //ExtractContent($query->post->post_content, 150);
$events[ $index ][ 'event_start_date' ] = get_post_meta( $event_id, 'event_start_date', true );
$events[ $index ][ 'event_end_date' ] = get_post_meta( $event_id, 'event_end_date', true );
$events[ $index ][ 'event_location' ] = get_post_meta( $event_id, 'event_location', true );
$events[ $index ][ 'event_link' ] = get_post_meta( $event_id, 'event_link', true );
$index ++;
}
usort($events, "cmp");
return $events;
}
function cmp($a, $b)
{
$adt = new DateTime($a['event_start_date']);
$ats = $adt->getTimestamp();
// echo
$bdt = new DateTime($b['event_start_date']);
$bts = $bdt->getTimestamp();
return strcmp($ats, $bts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment