Using Additional Custom Taxonomies
Events come with categories and tags. If you need another way to group them, such as audience, level or genre, attach a WordPress taxonomy to the event post type. Events Manager detects every taxonomy registered for events or locations and turns it into a search attribute for shortcodes and PHP, with no further code.
What you will build
A "Genre" taxonomy on events, and a list filtered by it: [events_list genre="jazz"].
You need somewhere to put PHP (Where to add PHP). If you would prefer no code, a plugin such as Custom Post Type UI or Pods can register a taxonomy and attach it to the Events and Locations post types. The search attribute works the same either way, so skip to Filter by the taxonomy.
Register a taxonomy for events
-
Add the code below to your plugin.
-
Go to Settings then Permalinks and click Save Changes, so the new taxonomy's addresses work.
-
Edit an event. A Genres box is in the editor, and Genres is in the Events menu.
add_action( 'init', 'my_em_register_genre_taxonomy', 20 );
function my_em_register_genre_taxonomy() {
if ( ! defined( 'EM_POST_TYPE_EVENT' ) ) {
return;
}
register_taxonomy( 'genre', array( EM_POST_TYPE_EVENT, 'event-recurring' ), array(
'labels' => array(
'name' => __( 'Genres', 'my-plugin' ),
'singular_name' => __( 'Genre', 'my-plugin' ),
),
'public' => true,
'hierarchical' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'events/genre' ),
) );
}
event-recurring is the post type of repeating events. Leave it out if your site doesn't use them. Add EM_POST_TYPE_LOCATION to the array to put the taxonomy on locations as well.
The priority of 20 runs after Events Manager has registered its post types on init. The rewrite argument must not be false: Events Manager ignores taxonomies that have no rewrite settings.
Attach an existing taxonomy
To reuse a taxonomy that already exists, attach it with register_taxonomy_for_object_type().
add_action( 'init', 'my_em_location_taxonomies', 100 );
function my_em_location_taxonomies() {
if ( ! defined( 'EM_TAXONOMY_CATEGORY' ) ) {
return;
}
register_taxonomy_for_object_type( EM_TAXONOMY_CATEGORY, EM_POST_TYPE_LOCATION );
register_taxonomy_for_object_type( EM_TAXONOMY_TAG, EM_POST_TYPE_LOCATION );
}
add_action( 'init', 'my_em_post_categories_on_events', 100 );
function my_em_post_categories_on_events() {
if ( ! defined( 'EM_POST_TYPE_EVENT' ) ) {
return;
}
register_taxonomy_for_object_type( 'category', EM_POST_TYPE_EVENT );
register_taxonomy_for_object_type( 'category', 'event-recurring' );
}
Filter by the taxonomy
The search attribute is named after the taxonomy, with any hyphens changed to underscores. Its value is one or more term slugs or IDs separated by commas. Put a minus sign before a term to exclude it.
| Taxonomy | Attribute |
|---|---|
genre | genre |
skill-level | skill_level |
category (post categories) | post_category |
post_tag (post tags) | post_tag |
Post categories get the post_ prefix because category already means event categories in Events Manager.
[events_list genre="jazz,-rehearsal"]
[events_calendar genre="jazz"]
[events_list post_category="featured"]
echo EM_Events::output( array( 'genre' => 'jazz,-rehearsal', 'scope' => 'future' ) );
A taxonomy filters the type of object it is attached to. [locations_list genre="jazz"] works only if the taxonomy is registered for locations, and it then looks at the locations' own terms, not at the terms of events held there.
What you don't get
A taxonomy added this way has search attributes and the standard WordPress term screens. It doesn't get the extras that event categories have: a color and image per term, placeholders such as #_EVENTCATEGORIES, or a field in the search form. For those, build on the EM_Taxonomy_Term and EM_Taxonomy_Terms classes, which EM_Category and EM_Categories extend, or print the terms yourself with a custom placeholder that calls get_the_term_list().
Test it
- Edit an event, add the genre
Jazz, and update it. - Put
[events_list genre="jazz"]on a page. Only that event is listed. - Change it to
[events_list genre="-jazz"]. Every upcoming event except that one is listed.