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 will keep the index lean and blocking the events in searching 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_do_not_index', 'rlv_no_past_events', 10, 2 );
add_filter( 'relevanssi_post_ok', 'rlv_no_past_events', 10, 2 );
 * Blocks past events from search results and indexing.
 * @param boolean $status  Should the post be indexed/searched or not?
 * @param int     $post_id The post ID.
 * @return boolean For relevanssi_post_ok, return false if the event has passed.
 *                 For relevanssi_do_not_index, return true 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() ) {
			if ( 'relevanssi_post_ok' === current_filter() ) {
				$status = false;
			} else {
				$status = true;
	return $status;

2 comments Events Calendar: Remove past events from search

Leave a Reply

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.