Skip to main content

Using Additional Custom Taxonomies

Events Manager automatically makes any taxonomy associated with events or locations searchable/filterable using our shortcodes and PHP listing functions.

info

Update : Since version 5.8 we have base classes you can extend and duplicate the features available in our own taxonomies, including uploadable images, color picking and associated placeholders. This tutorial is still accurate, but will be updated in the future to provide more information on extending our own classes. Meantime, developers will be interested in seeing how we do this with our own classes, such as with EM_Category and EM_Categories

Using other Plugins to help

There are various plugins which allow you to create custom taxonomies and associate them (or already present taxonomies) with existing custom post types. Since Events and Locations are custom post types, you can achieve the same effect using these plugins if you'd rather not add any code.

Two recommended plugins for the task are:

The end result is the same as coding this out, so we'll explain what to do once you've added a taxonomy to Events or Locations further down.

Examples : Add existing taxonomies

note

We're assuming you've already registered a taxonomy, or are using previously created taxonomies. We won't cover registering a new taxonomy, for that, see the already-comprehensive instructions on the Wordpress Codex._

The principle is exactly the same for any taxonomy you'd like to add, here's the code:

function my_em_own_taxonomy_register(){
register_taxonomy_for_object_type('custom_category',EM_POST_TYPE_EVENT);
register_taxonomy_for_object_type('custom_category',EM_POST_TYPE_LOCATION);
register_taxonomy_for_object_type('custom_category','event-recurring');
}
add_action('init','my_em_own_taxonomy_register',100);

Seriously, it's that easy. What we've done above is add the 'custom-category' taxonomy to both Events and Locations, and is now available to your shortcodes and other object functions as custom_category .

Here's another common example, adding the Event Tags and Categories that ship with Events Manager:

function my_em_own_taxonomy_register(){
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_own_taxonomy_register',100);

Notice the slight difference here, which is we're using constants to name the taxonomies, you could just as easily use 'event-categories' and 'event-tags', but since we define these as constants this is a 'configuration-safe' approach.

Exceptions : Post Tags and Categories

One final common example which has a slight twist - adding the normal posts category to your events and locations:

function my_em_own_taxonomy_register(){
register_taxonomy_for_object_type('category',EM_POST_TYPE_EVENT);
register_taxonomy_for_object_type('category',EM_POST_TYPE_LOCATION);
register_taxonomy_for_object_type('category','event-recurring');
}
add_action('init','my_em_own_taxonomy_register',100);

Now your Events and Locations can assign and search for post categories. The twist is when using this in your search attributes, since we already use category and tag, you need to use post_category and post_tag instead.

Using Custom Taxonomies in searches

Once you've added the taxonomy to Events or Locations, it's time to search! You can do this using shortcode or PHP. Here's an example of both ways to search the post categories we added above.

Shortcode:

[events_list post_category=”featured,-hidden”]
[locations_list post_category=”featured,-hidden”]

Code:

echo EM_Events::output( array('post_category'=>'featured,-hidden') );
echo EM_Locations::output( array('post_category'=>'featured,-hidden') );

The two examples above search for events and locations respectively which contain the featured category but not the hidden category.

Note that when filtering with taxonomies, both events and locations are not considered here, only one or the other. That means if you want to filter locations by a specific taxonomy, you use location functions and shortcode and vice-versa.