Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Created September 6, 2019 01:24
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 joshfeck/72021a1845bce177f559d2f9b363faaa to your computer and use it in GitHub Desktop.
Save joshfeck/72021a1845bce177f559d2f9b363faaa to your computer and use it in GitHub Desktop.
Change the next and previous post links sort order and filter out expired events for single espresso_events post types. Requires EE4 + a theme that uses the native next_post_link() and previous_post_link() functions.
<?php
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file
add_action( 'template_redirect', 'ee_custom_filtered_adjacent_post' );
function ee_custom_filtered_adjacent_post() {
add_filter( 'get_next_post_join', 'ee_custom_filtered_adjacent_post_join' );
add_filter( 'get_previous_post_join', 'ee_custom_filtered_adjacent_post_join' );
add_filter( 'get_next_post_where', 'ee_custom_filtered_adjacent_post_where' );
add_filter( 'get_previous_post_where', 'ee_custom_filtered_adjacent_post_where' );
add_filter( 'get_next_post_sort', 'ee_custom_filtered_adjacent_post_sort' );
add_filter( 'get_previous_post_sort', 'ee_custom_filtered_adjacent_post_sort' );
}
function ee_custom_filtered_adjacent_post_join($sql) {
global $wpdb;
if ( !is_main_query() || !is_singular( 'espresso_events' ) ) {
return $sql;
}
$sql .= ' INNER JOIN ' . EEM_Datetime::instance()->table() . ' ON ( p.ID = ' . EEM_Datetime::instance()->table() . '.' . EEM_Event::instance()->primary_key_name() . ' ) ';
return $sql;
}
function ee_custom_filtered_adjacent_post_where($sql) {
global $wpdb, $post;
if ( !is_main_query() || !is_singular( 'espresso_events' ) ) {
return $sql;
}
$sql .= ' AND ' . EEM_Datetime::instance()->table() . ".DTT_EVT_end > '" . current_time('mysql', true) . "' ";
return $sql;
}
function ee_custom_filtered_adjacent_post_sort($sql) {
global $wpdb;
if ( !is_main_query() || !is_singular( 'espresso_events' ) ) {
return $sql;
}
if ( current_filter() == 'get_next_post_sort' ) {
$sort = 'ASC';
} else {
$sort = 'DESC';
}
$sql = " ORDER BY {$wpdb->prefix}esp_datetime.DTT_EVT_start {$sort} LIMIT 1";
return $sql;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment