Cleaning up and sorting search results
-
Hi all,
I’d like to make the following adjustments to search results:
1. Sort by Event Start Date, ascending, so closer events come first
2. Filter out events in the pastI’ve got the following two filter which purport to do this. The first will order the search results by event start date:
function event_sort_order( $query ) { if( is_search() && ! is_single() ) { $query->set( 'orderby', 'meta_value' ); $query->set( 'meta_key', '_EventStartDate' ); $query->set( 'order', 'ASC' ); } return $query; } add_filter( 'pre_get_posts', 'event_sort_order', 99 );
However, it also results in any posts which aren’t events being filtered from the results. Anyone know what the query syntax is to say “order by start date – but only if you’re a tribe_event”?
This filter should remove all events where the event date is in the past (taken from a post on Tribe’s support forum):
function filter_tribe_all_occurences ($wp_query) { if ( !is_admin() && is_search() ) { $new_meta = array(); $today = new DateTime(); // Join with existing meta_query if(is_array($wp_query->meta_query)) $new_meta = $wp_query->meta_query; // Add new meta_query, select events ending from now forward $new_meta[] = array( 'key' => '_EventEndDate', 'type' => 'DATETIME', 'compare' => '>=', 'value' => $today->format('Y-m-d H:i:s') ); $wp_query->set( 'meta_query', $new_meta ); } return $wp_query; } add_filter('tribe_events_pre_get_posts', 'filter_tribe_all_occurences', 100);
Doesn’t seem to have any effect though. Anyone know how to fix it?
These two requirements seem like pretty basic and obvious features for an event plugin. What’s the chance of them being integrated into the plugin itself so I don’t have to code them myself?
- The topic ‘Cleaning up and sorting search results’ is closed to new replies.