Create Your Own Event Scope
The scope search attribute limits events to a date range. Events Manager comes with named scopes such as today, tomorrow, this-week, month and 3-months, and it also accepts two dates separated by a comma, as in scope="2027-06-01,2027-06-30".
Write your own scope when the range you need is relative to today and isn't one of the built-in names. This tutorial adds today-tomorrow, which matches events taking place today or tomorrow.
What you will build
A scope you can use as [events_list scope="today-tomorrow"], which also appears wherever Events Manager lists its scopes: the date filter above the events and bookings tables in wp-admin, and settings such as Default List Scope.
You need Events Manager 7.5 or later and somewhere to put PHP. See Where to add PHP.
Steps
-
Register the scope's name and label on the
em_get_scopesfilter. -
Supply the SQL condition for it on the
em_events_build_sql_conditionsfilter. -
Use the scope in a shortcode, block or PHP call.
add_filter( 'em_get_scopes', 'my_em_scopes' );
function my_em_scopes( $scopes ) {
$scopes['today-tomorrow'] = __( 'Today and tomorrow', 'my-plugin' );
return $scopes;
}
add_filter( 'em_events_build_sql_conditions', 'my_em_scope_conditions', 10, 2 );
function my_em_scope_conditions( $conditions, $args ) {
if ( empty( $args['scope'] ) || $args['scope'] !== 'today-tomorrow' ) {
return $conditions;
}
$today = wp_date( 'Y-m-d' );
$tomorrow = ( new DateTimeImmutable( 'tomorrow', wp_timezone() ) )->format( 'Y-m-d' );
$conditions['scope'] = "( event_start_date <= CAST('$tomorrow' AS DATE) AND event_end_date >= CAST('$today' AS DATE) )";
return $conditions;
}
How it works
em_get_scopes filters the array of scope names and labels. Adding yours puts it in the scope menus in wp-admin and the settings. You can also remove built-in scopes from the array to hide them from those menus.
em_events_build_sql_conditions filters the WHERE conditions of an event query, as an array of SQL fragments that Events Manager joins with AND. The scope key holds the date condition. The callback returns early unless the query asked for your scope, then replaces that key.
The condition matches any event that overlaps the two days: it starts on or before tomorrow and ends on or after today. That includes a multi-day event that began last week and is still running. Keep the outer parentheses, so the fragment stays one unit when it is joined to the other conditions.
Both dates are produced by PHP from the site's clock and are in a fixed Y-m-d form, so nothing from the request reaches the SQL. If you build a scope from a value a visitor supplies, pass it through $wpdb->prepare().
event_start_date and event_end_date hold each event's dates in the event's own timezone. wp_date() and wp_timezone() give today's date in the site's timezone. On a site with events in several timezones, "today" is therefore the site's today.
Test it
- Add
[events_list scope="today-tomorrow"]to a page and view it. It lists only events that take place today or tomorrow, or shows the "no events" message if there are none. - Create a test event dated tomorrow and reload the page. It appears.
- Change its date to three days from now. It disappears.