Posted on

Events Calendar: Remove past events from search

If you have events on your site, you might not want to show the past events in the search. This is easy to do with Relevanssi. It can be done in two ways: either you block the past events in indexing or in searching. In most cases, the best solution is to do both. Blocking the events in indexing with relevanssi_indexing_restriction will keep the index lean and blocking the events in searching with relevanssi_post_ok will make sure past events will be removed immediately from the search results.

These instructions apply to The Events Calendar plugin, which stores the event end date in the custom field _EventEndDate. It can be applied to other event calendar plugins by changing how the end date is fetched.

add_filter( 'relevanssi_post_ok', 'rlv_no_past_events', 10, 2 );
add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_past_events' );

 * Blocks past events from search results.
 * @param boolean $status  Should the post be searched or not?
 * @param int     $post_id The post ID.
 * @return boolean Return false if the event has passed.
function rlv_no_past_events( $status, $post_id ) {
	$end_date = get_post_meta( $post_id, '_EventEndDate', true );
	if ( $end_date ) {
		if ( strtotime( $end_date ) < time() ) {
			$status = false;
	return $status;

 * Removes past events from indexing.
 * @param array $restriction The MySQL restriction and an explanation.
 * @return array The restriction set with event restriction included.
function rlv_exclude_past_events( $restriction ) {
	global $wpdb;
	$restriction['mysql']  .= " AND post.ID NOT IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_EventEndDate' AND meta_value < NOW())";
	$restriction['reason'] .= ' Past event';
	return $restriction;

2 comments Events Calendar: Remove past events from search

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.