Blog
Events Manager Plugin – Hack to display events by categories
- February 15th, 2010
- WordPress
- 26 Comments
I have a client who uses the Events Manager plugin by Davide Benini and Marcus Skyes to manage events on their site.
Unfortunately, the plug-in doesn’t let you display events by category (which is weird, otherwise, why have the categories?) but anyway, I wrote/hacked together a custom function and WordPress shortcode today to display the events by category.
Now, I know there are going to be the naysayers who say “This code isn’t very pretty”. Yeah, well maybe not but it works. And if you can improve on it, DO! I’m totally open to it. For instance, the edit I did makes more than one query to the DB which isn’t optimal.
Okay, so without further ado, here it is. Open up the file in your events-manager/ folder called ‘marcus-extras.php’. Around line 264 you’ll find a function called dbem_get_category(). I added my custom function and shortcode right below it :
define('DBEM_EVENTS_TBNAME', 'dbem_events');
And then:
function dbem_display_categories() { global $wpdb; $categories_table = $wpdb->prefix.DBEM_CATEGORIES_TBNAME; $events_table = $wpdb->prefix.DBEM_EVENTS_TBNAME; $sql_categories = mysql_query("SELECT * FROM $categories_table ORDER BY category_id"); while($category = mysql_fetch_assoc($sql_categories)) { extract($category); $events = mysql_query("SELECT * FROM $events_table WHERE event_category_id = '".mysql_real_escape_string($category_id)."' ORDER BY event_start_date ASC"); $category_list .= "<div class=\"event\">"; $category_list .= "<h2>$category_name</h2>"; while($event = mysql_fetch_assoc($events)) { extract($event); $start_date = date('m/d/Y',strtotime($event_start_date)); $end_date = date('m/d/Y',strtotime($event_end_date)); $category_list .= "<li>Event: <strong><a href=\"http://www.wilderness-adventure.com/camp-dates/?event_id=$event_id\" title=\"$event_name\">$event_name</a></strong> <ul> <li>Dates: $start_date - $end_date</li> </ul></li>"; } $category_list .= "</div>"; } echo $category_list; } add_shortcode('dbem_events_by_category', 'dbem_display_categories');
The shortcode is – [dbem_events_by_category] and you can just insert it into your post or page whereever you want it to go.
By default it outputs ALL events under ALL categories but you could hack it up pretty easily to only display one category, etc. I may do that later if enough people ask for it but it’s not what I needed so, didn’t get done. :)
Hope this helps some folks. Let me know if you have any questions!
Comments (26)
Leave a Comment