Creating the Difference

Use WordPress Categories

Posted on: 14-11-2018 Last modified: 03-07-2021
Categories: PHP, Snippets Tags:

Filter EM Events using WP Categories. By default Events Manager does not support the use of “normal” WordPress categories in your events. But are you missing that feature? Agreed, Events Manager provides a nifty solution with their Event Categories. The problem with those is though, that even though they work great, they can not be (easily) used outside of the EM loop. :-(

Another benefit to use WP Categories with EM.
Apart from using categories outside of the EM loop, using WP native categories, also automatically adds them to the Events List. Unlike Event Categories, WP categories can be used to filter the list.

So, if you are like me and have public events as well as private events, you might need to filter the list every now and then. Using WP Categories in Events Manager allows you to do that!

How to use WordPress categories in Events Manager?


Step 1: Add WP Categories to Events Manager

To add a side meta box to your event edit page, just add this to your functions.php:Click here to learn how to safely add your code to WordPress.

[php] function stonehenge_em_use_wp_categories() { $supported_array = (EM_MS_GLOBAL && !is_main_site()) ? array() : array(EM_POST_TYPE_EVENT,’event-recurring’); register_taxonomy_for_object_type(‘category’,EM_POST_TYPE_EVENT); register_taxonomy_for_object_type(‘category’,’event-recurring’); // Uncomment next line to also use for EM Locations. //register_taxonomy_for_object_type(‘category’,EM_POST_TYPE_LOCATION); } add_action(‘init’, ‘stonehenge_em_use_wp_categories’, 100); [/php]

Step 2: Rename WP Category Name

Unfortunately some Events Manager translations translate ‘Event Category’ the same as ‘Category’. When you add the WP Categories to Events Manager, you will end up with two identically named meta boxes. That would be confusing, right? That is why we will change the name of the WP Categories section.

[php] function stonehenge_em_rename_wp_categories() { global $wp_taxonomies; $categories = $wp_taxonomies[‘category’]; $categories->label = __(‘Blog Categories’, ‘your-textdomain’); $categories->labels->singular_name = __(‘Blog Category’, ‘your-textdomain’); $categories->labels->name = $categories->label; $categories->labels->menu_name = $categories->label; } add_action(‘init’, ‘stonehenge_em_rename_wp_categories’); [/php]

Step 3: Create a custom placeholder

If you would like to also use the WP Cagegory in the front-end, it is easiest if you could use it just like any other Event info. So we’ll create a custom placeholder that will output the WP Category.

[php] function stonehenge_em_placeholder_wp_category($replace, $EM_Event, $result) { if( $result === “#_WPCATEGORY”) { $post_id = $EM_Event->post_id; $categories = get_the_category( $post_id ); foreach( $categories as $category ) { $replace = $category->name; } } return $replace; } add_filter(’em_event_output_placeholder’, ‘stonehenge_em_placeholder_wp_category’, 10, 3); [/php]

Step 4: WP MultiSite compatible.

This snippet also works on WordPress MultiSite installations. Please note that, unlike Event Categories, the WP Categories are maintained per blog.

⇐ Previous
Next ⇒