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;
}

6 comments Events Calendar: Remove past events from search

  1. Somethign that tripped me up here: the $post_id passed to the relevanssi_post_ok callback is a string, not an integer.

  2. In fact, I can see that my callback returns false for the results I want to exclude, but they still appear in search results. I’ve flushed caches, etc. but still no change.

    1. Charles, $match->doc indeed isn’t an integer, because it can also be something like “u_123” for users or “**category**123” for categories in Relevanssi Premium.

      If the relevanssi_post_ok function actually runs during the search and returns false, that should eliminate the posts from the results. Make sure that your search is actually powered by Relevanssi. Do the posts appear in the Relevanssi admin search (Dashboard > Admin search), or does the function work there? If you are sure the search is Relevanssi, then check if something else in the relevanssi_post_ok hook doesn’t convert the value back to true. Try moving your function to a very high priority, for example.

  3. Well that’s weird. That comment I just posted is a draft I wrote in this form a few days ago. I stopped and did more research. I refreshed this page and the form was empty, I typed a new message, sent that and it instead posted this older message.

    Anyhow, I reached out to The Events Calendar folks about hiding past single events in a still active recurring events series and they said:

    > Thanks for reaching out. At this time, that is not really possible. However, the good news is that it soon will be. One of the main features we have been focusing on is improving recurring events. We are getting pretty close to releasing a fully rebuilt and feature rich recurring events platform. While I cannot give any real timeline for release, I do believe the target goal is by the end of this year.

Leave a Reply to Charles Cancel 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 *