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)
Paul -
February 16, 2010
"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"
I'd really like the option to display just one chosen category (as mentioned above) – any additional help much appreciated. I am committed to using this plugin on a client site so need to get it just right :-)
My recent post new website for top Italian cook Silvana de Soissons
croakingtoad -
February 16, 2010
I probably need to code this as a plug-in or you may want to add it to your theme function.php file. Adding it to the plug-in file means when you upgrade the plug-in, this hack gets overwritten. FYI.
Willem -
February 16, 2010
Hi,
nice work!
I use event manager ver 2.1a (available by choosing "other versions" on the download site) and i have done it a bit differently.
You can create a page wich contains this text:
[EVENTS_LIST LIMIT=50 CATEGORY=1] the limits part is optional, but i my case i have a lot of events and by default i could only get 10 events to show.
You can add more lines with a different category id, so this would in effect sort your events by category.
In my case i just made more pages, each with a different category.
Only thing is that you do not assign your events page to be used as the page to show events (in event manager settings) but rather use a dummy page for that and select "no" on the "Show events page in lists?" option.
Otherwise your events list will not show unless you are logged in into wordpress…
My main "problem" with these categories is that they do not match with the wordpress categories, you know a fix for that maybe?
grt
Willem
Netherlands
Paul -
February 17, 2010
I don't see this version listed…
@mikeyleung -
February 24, 2010
You have to look very carefully on the right, where the plugin links and author links are. took me a while to find it too.
der marKo -
February 22, 2010
hi willem,
first, i must say I have no idea about php databases. i understand it correctly that you have just created a new page and then your text [EVENTS_LIST CATEGORY LIMIT = 50 = 1] have entered? because for me it works like that. I get to my new site only [EVENTS_LIST CATEGORY = 1].
what have I done wrong? please explain it as simple. : o)
thanks
greetings from cologne
der marKo
@mikeyleung -
February 24, 2010
Note that the [events_list] shortcode must be in small caps.
Paul -
February 16, 2010
perhaps adding to the theme function.php file might be best in my situation – however, open to suggestions…
My recent post new website for top Italian cook Silvana de Soissons
croakingtoad -
February 16, 2010
I think you're right. Also note @Willem's suggestion. I think that would work for what you are trying to do?
Paul -
February 16, 2010
hmmm – is there an easy way to discover what version I'm on?!
My recent post new website for top Italian cook Silvana de Soissons
croakingtoad -
February 16, 2010
Yes, just go to your plugins page and look under the description.
Heather Jacobson -
February 16, 2010
I hate when people tell me that they've made something better. Because now? I want to use this plug in. But I don't have any events.
My recent post Most TV Stations Don’t “Get” Twitter…
Willem -
February 18, 2010
You can download ver. 2.1a from the wordpress plugins download site by choosing "other versions" which leads to a development version (being 2.1a).
Joe Crawford -
February 21, 2010
Nice job! The suggestion I have would be to share your code with a tool like GitHub. If you don't want to share it as a project, you could do it as a Gist, which is for smaller chunks of code, and may fit all the relevant code for you.
croakingtoad -
February 22, 2010
Thanks Joe. There's definitely room for improvement as I'm sure you'll note but not too shabby for what it is I think!
Joe Crawford -
February 22, 2010
Absolutely, whatever helps get stuff done is usually fine by me!
@mikeyleung -
February 24, 2010
Hi guys,
Just wondering, does adding this code allow one to display the events by category in the calendars, say via permalink? I haven't yet managed to get Events Manager installed correctly on my site yet so I'm still guessing at what it can and cannot do.
croakingtoad -
February 24, 2010
This is one way Event Calendar displays events on a website:
Wilderness Adventure Camp Dates
Michiko Wendel -
March 7, 2010
That was brilliant.
Carey -
March 9, 2010
I'm wondering if there is a way to add the scope for "future" events, because what I get right now is ALL events. I tried using this: [events_list limit=50 category=2 scope=future]
but, it did not work…
Paul -
May 20, 2010
Hi guys – back again.
Anyone worked out how to display events based on custom attributes? For example, if I have a custom attribute of "lights off" and a value of "yes" stored for that attribute, how can I use the [events_list] shortcode to only show events where the attribute is "yes" ? Any takers?
mtboston -
June 23, 2010
This is great! but I have the same issues as some of the above!
1. how do i show only one category
2. show only future events
thanks for ANY HELP!
Nicole -
July 6, 2010
I made some changes that allow you to add category to the shortcode:
[dbem_events_by_category category=1]
And it shows "future" events (note the $scope variable).
Here's the code from above modified:
<code>
define('DBEM_EVENTS_TBNAME', 'dbem_events');
function dbem_display_categories($atts) {
global $wpdb;
extract(shortcode_atts(array(
'category' => 'category',
), $atts));
$categories_table = $wpdb->prefix.DBEM_CATEGORIES_TBNAME;
$events_table = $wpdb->prefix.DBEM_EVENTS_TBNAME;
$timestamp = time ();
$date_time_array = getdate ( $timestamp );
$hours = $date_time_array ['hours'];
$minutes = $date_time_array ['minutes'];
$seconds = $date_time_array ['seconds'];
$month = $date_time_array ['mon'];
$day = $date_time_array ['mday'];
$year = $date_time_array ['year'];
$today = strftime ( '%Y-%m-%d', mktime ( $hours, $minutes, $seconds, $month, $day, $year ) );
$scope = "(event_start_date >= '$today' OR (event_end_date >= '$today' AND event_end_date != '0000-00-00' AND event_end_date IS NOT NULL))";
$sql_categories = mysql_query("SELECT * FROM $categories_table WHERE category_id=$category 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)."' AND $scope ORDER BY event_start_date ASC");
$category_list .= "<div class="event">";
$category_list .= "<ul class='cal-list'>";
$num_rows = mysql_num_rows($events);
if ($num_rows==0) {
$category_list .= "No events coming up.";
} else {
while($event = mysql_fetch_assoc($events)) {
extract($event);
$start_date = date('F j',strtotime($event_start_date));
$end_date = date('F j Y',strtotime($event_end_date));
$category_list .= "<li class='$category_name'>$start_date $event_name
";
}
}
$category_list .= "</div>";
echo $category_list;
}
}
add_shortcode('dbem_events_by_category', 'dbem_display_categories');
</code>
My recent post In This Economy- Small Businesses Can’t Afford To Skimp On Design
Marty Martin -
September 17, 2010
For anyone who is interested, there’s a new premium event management plugin available. It looks pretty promising, check it out- http://mear.is/espresso
Sarah -
January 24, 2012
Nice stuff, thanks. Any idea how this could be done to display a list of event tags, or a tag cloud for event tags?
Sarah -
January 25, 2012
Answered my own question. With WordPress 3.1, you’re able to pass an array of taxonomy names to wp_tag_cloud. So to have regular post tags and Events Manager tags, do: array(‘event-tags’, ‘post_tag’), ‘number’=>200, ‘unit’=>’px’, ‘smallest’=>9, ‘largest’=>36)); ?>
Leave a Comment