Creating Custom Event Search Attributes

This tutorial forms part of our series of tutorials for creating an custom add-on for Events Manager called “Styles”, where we will be able to select from a list of styles during event registration.

If you create your own custom information for events, you may want to allow Events Manager to search by this attribute. With a few lines of code, you can hook your new features straight into the core of Events Manager’s event search engine and provide a seamless integration.

In this tutorial we’ll be extending our styles add-on which follows on from our previous tutorial on saving custom event information to the database.

The first step to take is that we need to hook into the default search filter, so that our new search attribute isn’t ignored when Events Manager creates an events search query:

function my_em_styles_get_default_search($searches, $array){
	if( !empty($array['style']) && is_numeric($array['style']) ){
		$searches['style'] = $array['style'];
	return $searches;

The first line hooks into the EM_Events default search attributes filter, which takes a search request, filters and removes unwanted/bad search variables. At this point in the filter, $searches represents an associative array of clean attributes, and $array is the originally supplied array. We run a quick check to see if the style key exists in the original array and copy it over the the clean $searches array, before we pass it back to the filter function.

This sort of logic works on most object searches. All objects that search, e.g. EM_Events, EM_Locations, EM_Categories have a filter in the form of em_object_get_default_search, which add extra attributes not present in the master function located in EM_Object::get_default_search()

The next step is to add the function Events Manager needs to narrow down its results. In this case, we’re going to filter the results of an EM_Events::get() request via the em_events_get filter:

function my_em_styles_events_get($events, $args){
	if( !empty($args['style']) && is_numeric($args['style']) ){
		foreach($events as $event_key => $EM_Event){
			if( !in_array($args['style'],$EM_Event->styles) ){
	return $events;

This filter passes an array of EM_Event objects that matched the supplied attributes during an events search, and $args represents the arguments used to obtain this list of events. Since events already loaded our style information we can loop through all these events and remove the events that don’t have the requested style.

Alternate method

In this case, since we already loaded event styles another way, there was little point in joining a new table and making a more complicated SQL search. There is just as often the need to add an SQL condition to the search query rather than filtering the results. This is also very straightforward, and can be viewed in the implementation of creating a custom event scope.

Using this new attribute

The new attribute you just created is usable by event, location and calendar related shortcodes and functions. So, for example you could bring up a list of locations that have upcoming events with a style of id 5 like so:

[locations_list style=5]

In our next tutorial we’ll be adding a custom field to the events search form so that users can make the best of this new filtering.