• Resolved Webreaper

    (@webreaper)


    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 past

    I’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?

Viewing 1 replies (of 1 total)
  • @webreaper, we are unable to provide assistance with custom code like this.

    I will point out, however, that your first snippet utilizing pre_get_posts may be including post types besides our events and therefore some of the posts in there may not have our plugin’s meta key you’re trying to use.

    If you need some coding help, you may want to ask your developer or reference our documentation and list of known customizers.

Viewing 1 replies (of 1 total)
  • The topic ‘Cleaning up and sorting search results’ is closed to new replies.