Created
July 25, 2016 19:03
-
-
Save joshfeck/a8e28b9cfecd89308fbea4c152f546c9 to your computer and use it in GitHub Desktop.
Change the next and previous post links sort order for single espresso_events post types. Requires EE4 + a theme that uses the native next_post_link() and previous_post_link() functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file | |
function ee_custom_adjacent_post() { | |
add_filter( 'get_next_post_join', 'ee_custom_adjacent_post_join' ); | |
add_filter( 'get_previous_post_join', 'ee_custom_adjacent_post_join' ); | |
add_filter( 'get_next_post_where', 'ee_custom_adjacent_post_where' ); | |
add_filter( 'get_previous_post_where', 'ee_custom_adjacent_post_where' ); | |
add_filter( 'get_next_post_sort', 'ee_custom_adjacent_post_sort' ); | |
add_filter( 'get_previous_post_sort', 'ee_custom_adjacent_post_sort' ); | |
} | |
add_action( 'template_redirect', 'ee_custom_adjacent_post' ); | |
function ee_custom_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_adjacent_post_where($sql) { | |
global $wpdb, $post; | |
if ( !is_main_query() || !is_singular( 'espresso_events' ) ) { | |
return $sql; | |
} | |
// Pull the datetimes for this event order by start_date/time | |
$datetimes = EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $post->ID, true, false ); | |
// Get the last datetime and use that one. | |
$datetime = end( $datetimes ); | |
if ( $datetime instanceof EE_Datetime ) { | |
$current_event_start_date = $datetime->get_raw('DTT_EVT_start'); | |
$current_event_start_date = date( 'Y-m-d H:i:s', $current_event_start_date ); | |
if ( current_filter() == 'get_next_post_where' ) { | |
$compare = '>'; | |
} else { | |
$compare = '<'; | |
} | |
$sql = " WHERE {$wpdb->prefix}esp_datetime.DTT_EVT_start {$compare} '" . $current_event_start_date . "' AND p.post_type = 'espresso_events' AND ( p.post_status = 'publish' ) "; | |
} | |
return $sql; | |
} | |
function ee_custom_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