Skip to content

Instantly share code, notes, and snippets.

@barryhughes
Created April 2, 2018 22:36
Show Gist options
  • Save barryhughes/44a277f7472ba633db1ee2ea0f91c60f to your computer and use it in GitHub Desktop.
Save barryhughes/44a277f7472ba633db1ee2ea0f91c60f to your computer and use it in GitHub Desktop.
<?php
function ecp_fix_tag_order( $sql, $query ) {
// Only modify the main query, if it is a tag archive query
if ( ! $query->is_main_query() || ! $query->is_tag() ) {
return $sql;
}
$has_event_start_date = (bool) strpos( $sql, 'AS EventStartDate' );
$orders_by_post_date = (bool) strpos( $sql, 'ORDER BY post_date DESC' );
if ( $has_event_start_date && $orders_by_post_date ) {
$sql = str_replace( 'ORDER BY post_date DESC', 'ORDER BY EventStartDate DESC', $sql );
}
return $sql;
}
add_filter( 'posts_request', 'ecp_fix_tag_order', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment