Simple event list update
-
Thanks to gregoire1974 for creating the ep_simple_list shortcode. I am using it to show the “run” of shows (plays). As such I wanted some additional information printed.
I have modified it slightly and am sharing the updated version. This version adds the event end date, can optionally include the organizer name and you can choose to use the event start date or end date to determine if the event is a past event.
I’d like to add a link from the organizer name to the organizer page in event prime when the organizer is included, but seems to be above my very limited php abilities.
To prevent mix-ups the original shortcode, I have named this version “ep_simple_list3”. Here’s the updated code:
/**
* Shortcode: [ep_simple_list3]
* Displays a simplified list of EventPrime events, grouped into upcoming and past events.
*
* Shortcode attributes:
* - type="" → event type slug (EventPrime taxonomy)
* - limit=-1 → maximum number of events to display (-1 = no limit)
* - order="DESC" → sorting order based on start date
* - date="true" → display event date
* - venue="true" → display event venue
* - organizer="true" → display organizer
* - upcoming=-1 → -1 = all events, 0 = past only, 1 = upcoming only
* - date_test="start" → use "start" date or "end" date for upcoming test
*/
function simple_eventprime_list3( $atts ) {
/**
* Define shortcode attributes and defaults.
*/
$atts = shortcode_atts( array(
'type' => '',
'limit' => -1,
'order' => 'DESC',
'date' => 'true',
'venue' => 'true',
'organizer' => 'true',
'upcoming' => -1, // Default: show both upcoming and past events
'date_test' => 'start',
), $atts );
/**
* Query all EventPrime events.
* We fetch all events and handle date filtering manually.
*/
$args = array(
'post_type' => 'em_event',
'posts_per_page' => -1,
'meta_key' => 'em_start_date_time',
'orderby' => 'meta_value_num',
'order' => $atts['order'],
);
/**
* If an event type is provided, filter by taxonomy.
*/
if ( ! empty( $atts['type'] ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'em_event_type',
'field' => 'slug',
'terms' => $atts['type'],
),
);
}
$query = new WP_Query( $args );
/**
* Prepare arrays for upcoming and past events.
*/
$now = current_time('timestamp');
$upcoming_events = [];
$past_events = [];
/**
* Retrieve the page used to display single EventPrime events.
* Example: /all-events/?event=123
*/
$listing_page = get_page_by_path('all-events');
/**
* Loop through events and separate them based on date.
*/
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$event_id = get_the_ID();
if ( $atts['date_test'] === "end" ) {
$timestamp = get_post_meta( $event_id, 'em_end_date_time', true );
} else {
$timestamp = get_post_meta( $event_id, 'em_start_date_time', true );
}
if ( ! $timestamp ) continue;
// Classify event as upcoming or past
if ( $timestamp >= $now ) {
$upcoming_events[] = $event_id;
} else {
$past_events[] = $event_id;
}
}
}
wp_reset_postdata();
/**
* Start output buffering to return clean HTML.
*/
ob_start();
/**
* Helper function to output an event list section.
*/
$display_events = function( $event_ids, $title ) use ( $atts, $listing_page ) {
if ( empty( $event_ids ) ) return;
// Section title (e.g., "Upcoming Performances")
echo '<h5 style="font-weight:bold;">' . esc_html($title) . '</h5>';
echo '<ul class="simple-eventprime-list">';
$count = 0;
foreach ( $event_ids as $event_id ) {
// Apply "limit" attribute
if ( $atts['limit'] > 0 && $count >= $atts['limit'] ) break;
$count++;
$event_title = get_the_title( $event_id );
/**
* Build event link
* Preference: EventPrime listing page with ?event=ID
* Fallback: event permalink
*/
if ( $listing_page ) {
$event_link = add_query_arg('event', $event_id, get_permalink($listing_page->ID));
} else {
$event_link = get_permalink($event_id);
}
echo '<li>';
echo '<strong><a class="simple-eventprime-link" href="' . esc_url($event_link) . '"><em>' . esc_html($event_title) . '</em></a></strong>';
/**
* Display date if enabled.
*/
if ( $atts['date'] === 'true' ) {
$timestamp = get_post_meta( $event_id, 'em_start_date_time', true );
if ( $timestamp ) {
echo ': ' . date('m/d/Y', $timestamp) . ' ' ;
}
$timestamp = get_post_meta( $event_id, 'em_end_date_time', true );
if ( $timestamp ) {
echo ' - ' . date('m/d/Y', $timestamp) . ' ' ;
}
}
/**
* Display organizer if enabled.
* Uses EventPrime taxonomy: em_event_organizer
*/
if ( $atts['organizer'] === 'true' ) {
$venue_terms = get_the_terms( $event_id, 'em_event_organizer' );
if ( $venue_terms && ! is_wp_error( $venue_terms ) ) {
$venue_name = implode( ', ', wp_list_pluck( $venue_terms, 'name' ) );
echo ' presented by <b>' . esc_html( $venue_name ) . '</b>';
}
}
/**
* Display venue if enabled.
* Uses EventPrime taxonomy: em_venue
*/
if ( $atts['venue'] === 'true' ) {
$venue_terms = get_the_terms( $event_id, 'em_venue' );
if ( $venue_terms && ! is_wp_error( $venue_terms ) ) {
$venue_name = implode( ', ', wp_list_pluck( $venue_terms, 'name' ) );
echo ' at ' . esc_html( $venue_name ) . ' ';
}
}
echo '</li>';
}
echo '</ul>';
};
/**
* Display lists depending on "upcoming" attribute.
*/
if ( $atts['upcoming'] == -1 ) {
// Display both upcoming and past events
$display_events( $upcoming_events, 'Upcoming Performances' );
$display_events( $past_events, 'Past Performances' );
} elseif ( $atts['upcoming'] == 1 ) {
// Upcoming only
$display_events( $upcoming_events, 'Upcoming Performances' );
} elseif ( $atts['upcoming'] == 0 ) {
// Past only
$display_events( $past_events, 'Past Performances' );
}
/**
* Fallback message if no events are found.
*/
if ( empty($upcoming_events) && empty($past_events) ) {
echo '<p class="simple-eventprime-none">No events found.</p>';
}
return ob_get_clean();
}
add_shortcode( 'ep_simple_list3', 'simple_eventprime_list3' );
You must be logged in to reply to this topic.