Skip to main content

Creating Custom Event Search Attributes

Build an add-on

Part 5 of 11 in the Build an add-on series. Previous: Load event data. Next: Search form fields.

Events Manager filters events through search attributes such as category, scope and town. This part adds one of your own, style, so that [events_list style="1"] lists the events with style 1.

Built-in alternative

A custom taxonomy registered for events becomes a search attribute automatically. See Extra taxonomies. For a new date range, see Custom event scopes.

What you will build​

A style attribute that works in the event, calendar and location shortcodes, in the template tags, and in EM_Events::get().

Before you start

Complete part 4 and give at least one upcoming event a style.

Steps​

  1. Add these lines inside my_em_styles_init():

    add_filter( 'em_events_get_default_search', 'my_em_styles_get_default_search', 10, 2 );
    add_filter( 'em_calendar_get_default_search', 'my_em_styles_get_default_search', 10, 2 );
    add_filter( 'em_events_build_sql_conditions', 'my_em_styles_build_sql_conditions', 10, 2 );
    add_filter( 'em_locations_get_default_search', 'my_em_styles_get_default_search', 10, 2 );
    add_filter( 'em_locations_build_sql_conditions', 'my_em_styles_build_sql_conditions', 10, 2 );
    add_filter( 'em_locations_get_join_events_table', 'my_em_styles_locations_join_events_table', 10, 2 );
  2. Add the functions below to the end of the file.

Part 5: the style search attribute
function my_em_styles_get_default_search( $args, $array ) {
$args['style'] = false;
if ( ! empty( $array['style'] ) && is_numeric( $array['style'] ) ) {
$args['style'] = absint( $array['style'] );
}
return $args;
}

function my_em_styles_build_sql_conditions( $conditions, $args ) {
global $wpdb;
if ( ! empty( $args['style'] ) && is_numeric( $args['style'] ) ) {
$subquery = $wpdb->prepare( 'SELECT object_id FROM ' . EM_META_TABLE . " WHERE meta_key = 'event-style' AND meta_value = %s", absint( $args['style'] ) );
$conditions['style'] = EM_EVENTS_TABLE . ".event_id IN ( $subquery )";
}
return $conditions;
}

function my_em_styles_locations_join_events_table( $join, $args ) {
return ! empty( $args['style'] ) ? true : $join;
}

How it works​

A search goes through two stages, and the attribute has to be added to both.

Stage 1: accepting the attribute. Every query starts by passing its arguments through get_default_search(), which drops anything it doesn't recognize and fills in defaults. The em_events_get_default_search filter receives the cleaned arguments ($args) and what the caller originally passed ($array). The callback adds style with a default of false, and copies the caller's value across only if it is numeric. The calendar and locations classes have their own versions of the filter with the same arguments, so one callback serves all three.

Registering the attribute here is also what lets it arrive from a search form request, which part 6 uses.

Stage 2: changing the query. em_events_build_sql_conditions receives an array of SQL WHERE fragments, which Events Manager joins with AND. The callback adds one that limits event_id to the events with a matching row in the meta table. The value goes through $wpdb->prepare(), and the column is written with its table name (EM_EVENTS_TABLE . '.event_id') because some queries join other tables that also have an event_id column.

Locations. A location has no style of its own, so style on a location search means "locations that have an event with this style". Location queries only join the events table when they need to, and em_locations_get_join_events_table is where you tell them they do. With the join in place, the same condition works.

Use the attribute​

In a shortcode:

[events_list style="1" scope="future"]
[locations_list style="1"]
[events_calendar style="2"]

In PHP:

$events = EM_Events::get( array( 'style' => 1, 'scope' => 'future' ) );
echo EM_Events::output( array( 'style' => 1, 'limit' => 5 ) );

Test it​

  1. Add [events_list style="1"] to a page. Only events with style 1 are listed.
  2. Change it to [events_list style="999"]. The page shows the "no events" message.
  3. Change it to [events_list style="abc"]. The value is not numeric, so it is ignored and all upcoming events are listed.

Next​

Part 6: Search form fields