Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Last active February 5, 2020 22:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfeck/2f8c67a51ebeec117c7dc3a5cb4e0051 to your computer and use it in GitHub Desktop.
Save joshfeck/2f8c67a51ebeec117c7dc3a5cb4e0051 to your computer and use it in GitHub Desktop.
This plugin adds a [espresso_past_events] shortcode that you can add to a post or page to display a list of past events.
<?php
/**
* @package EE_Event_List_Past_Events_Archives_Shortcode
* @version 1.0
*/
/*
Plugin Name: Event Espresso Past Events List Shortcode
Plugin URI: https://github.com/eventespresso/ee-code-snippet-library
Description: Add [espresso_past_events] shortcode to a post or page to display a list of past events.
Author: Josh Feck
Version: 1.0
Author URI: https://github.com/joshfeck
*/
function my_ee_past_events_shortcode($args) {
if(!class_exists('EventEspresso\core\domain\services\wp_queries\EventListQuery')){
return;
}
$args = shortcode_atts(
array(
'limit' => 100,
'show_expired' => TRUE,
'category_slug' => NULL,
'sort' => 'DESC',
'order_by' => 'start_date',
),
$args,
'espresso_past_events'
);
add_filter( 'posts_where', 'ee_pels_custom_posts_where_sql_for_only_expired' );
$loop = new EventEspresso\core\domain\services\wp_queries\EventListQuery( $args );
$big = 999999999; // need an unlikely integer
$pagination = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'show_all' => TRUE,
'end_size' => 10,
'mid_size' => 6,
'prev_next' => TRUE,
'prev_text' => '&lsaquo; PREV',
'next_text' => 'NEXT &rsaquo;',
'type' => 'plain',
'add_args' => FALSE,
'add_fragment' => ''
));
$html = include_once( plugin_dir_path( __FILE__ ) . '/shortcode-template.php' );
remove_filter( 'posts_where', 'ee_pels_custom_posts_where_sql_for_only_expired' );
wp_reset_postdata();
return $html;
}
add_shortcode('espresso_past_events', 'my_ee_past_events_shortcode');
function ee_pels_custom_posts_where_sql_for_only_expired($where) {
$where .= ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end < "' . current_time( 'mysql', TRUE ) . '" ';
return $where;
}
<?php /* template for past events shortcode */
$output = '<div class="module ee-events-archive event-list">';
$output .= '<article class="ee-event archived">';
if( $loop->have_posts() ) {
// loop through posts
while ( $loop->have_posts() ) : $loop->the_post();
$output .= '<strong class="event-title">' . get_the_title() . '</strong>';
$output .= '<div class="event-items">';
$output .= '<a class="view-details-btn" href="' . get_the_permalink() . '">View details</a>';
$output .= '<p class="event-time">Date: <span class="start-date">' . espresso_event_date('', ' ', false, false ) . '</span></p>';
$output .= '</div>';
endwhile;
}
$output .= '</article>';
// pagination links
$output .= ! empty( $pagination ) ? '<div class="ee-pagination-dv clear">' . $pagination . '</div>' : '';
$output .= '</div>';
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment