Integrating Post Categories & Tags With Events (2.x Builds Only)

Published on: November 9, 2011 |Tags: | Categories: Tutorial

This tutorial is for The Events Calendar 2.0.1 through 2.0.11 only. If you are using version 3.0 or beyond, event tags are built in. Category filters will be available with our upcoming Filters Bar.

Our first user-submitted Events Calendar tutorial is ready to rock…if you’ve ever wanted to know how to apply WordPress’ native categories & tags functionality to your events, this one’s for you. A big thanks to Steven Slack at S2 Webpress for putting this together. Check out the tutorial after the jump. Take it away, Steven…

—————————————————————————————

You may notice that the Events Calendar already comes with its own “custom taxonomy” named “event categories”. While this is a great feature you may find yourself wanting to include the wordpress native categories and tags that you use in your regular posts to also be associated with the events on the events calendar. Doing this will allow you to assign events to various categories also associated with your posts. You may have a slider or featured post plugin that uses categories to be featured on a slider and you need your events to be included. We are going to show you how to do this. So lets get started!

Be sure to back your site up before making any changes!

Modify your theme

In order to do this you need to modify your theme’s functions.php file. If you are using a child theme you may need to create a functions.php file if one doesn’t exist.  You will be adding some functionality to the Events Calendar within your theme. In which case you will not have to worry about upcoming updates to the Events Calendar overriding the changes you make as all your edits will take place within your theme.

There are two ways to edit your theme:

  1. Using WordPress’ built in editor
  2. Using an FTP client like filezilla or FireFTP

If you are already familiar with editing your theme’s files you can skip Method 1 and 2 to editing your themes files and adding Categories and Tags to the Events Calendar.

Method 1: Using the Theme Editor

If you already have a theme active on your WordPress install or if you are modifying a custom theme go to your admin menu and open Appearance => Editor.  Access the Theme Editor from the Administration > Appearance > Editor menu.

On the right side of your screen you will see a list of file names. Find the file named functions.php and click on it. This will load the file in the Editor window. NOTE:  If you do not have a functions.php file in the directory you must use an FTP client and create one ( go to method 2 below).

STOP! 

For the purposes of safety you should know that anything you modify here CANNOT be undone. So here is what you should do… backup your functions.php file by doing the following:

  • Open a simple text editor like notepad and copy and paste the original code into the text editor.
  • Save the file as original-functions.php ( this is your backup ).
  • Open a new document and paste the same code into it from your functions file. Title the file functions.php and save it.

This is the file you will be editing.

Method 2: Using an FTP Client

Connect to your web host using your FTP client of choice. Follow the path on your server to …/ wp-content / themes / your-theme / functions.php and download the file.

Save a copy as original-functions.php ( this is your backup ). Open a new document in a text editor and paste the same code into it from your functions file. Title the file functions.php and save it. This is the file you will be editing.

Editing your functions.php to Include Categories and Tags

Before making changes to your theme you should see the events calendar like this:


This is without post categories and tags.

Now lets make the changes! Open your functions.php file and scroll all the way to the bottom. Before the closing php tag ?>add the following code:

/* IMPORTANT !
* This function registers the same custom post type as the The Events Calendar plugin
* http://wordpress.org/extend/plugins/the-events-calendar/
* This also registers WordPress' native categories and tags while associating them with the Events Calendar Plugin
*/
add_action( 'init', 'add_calendar_taxonomy', 0 );
function add_calendar_taxonomy() {
register_post_type('tribe_events',array( // Registers Events Calendar Custom Post Type
'taxonomies' => array('category', 'post_tag') // This registers the native WordPress taxonomies with The Events Calendar
));
}

The code above does two things:

  1. Registers the the Events Calendar with your theme and
  2. Registers the Post Categories and Post Tags to all Events Calendar events

Save your functions.php file and upload it to your server via FTP. If you are using the WordPress editor simply cut and paste the code snippet above into the function.php file and click update file. You will now see Categories and Tags within your events calendar admin and event editor! Here’s them in the sidebar:

And here you can see the tags/categories within the event editor itself:

Now you can add your existing categories and tags to your events in the events calendar.

Tags: |

33 Responses to Integrating Post Categories & Tags With Events (2.x Builds Only)

  1. Adrian says

    Hi,

    Thanks for this!

    Can you advise if this method will work with Events Calendar Pro 1.3.3.
    If not, would you know of an alternative way to query events?

    Cheers.

    • Rob La Gatta says

      Hey Adrian. You’re welcome to give this a shot with 1.3.3, but the code has changed enough between the 1.x releases and 2.0 that I unfortunately doubt it’ll function. Have you tried browsing the old 1.3.3 forums (http://support.makedesignnotwar.com/categories/e) to see if someone proposed a solution there that might work? That’s sadly my best suggestion at this point.

  2. Mihajlo says

    Dear thank you for such wonderful plugin. I need to show my events on the front page and rss feed but couldn’t do it as I am doing this by another plugin which specify which categories to show on home page. With this code I should be able to do it. I can see the regular post categories under the Event’s posts. However, there two problems:

    1) When I put the code, the layout of Events admin area mess-up – columns size automatically resize and mess up…

    2) If I add regular categories to the Event post it still don’t show on the front page.

    Please, help!

    Many thanks!

    • Rob La Gatta says

      This is bizarre; the second issue may be caused by another plugin or your theme (ie if it’s set to just show 1 category per post or something along those lines). Any chance you could test whether this is a plugin conflict?

      #1 — are you saying this is what you see on the backend, when creating an event? I haven’t heard of this issue before….a screenshot showing what you’re seeing would definitely be helpful.

    • Jonah says

      Hi Mihajlo,

      I wonder if the query for your home page posts is specifying a post type and not including the events post type? I.e. it’s querying a post type and categories and so doesn’t pull in events… Hard to say without looking at some code.

      No idea why the admin would be getting messed up. I’d want to take a look at the code for this too. Can you provide me WP admin/FTP access? Please email jonahcoyote [at] gmail [dot] com and I’d be happy to take a look.

      Regards,
      Jonah

  3. esteban says

    I followed this tutorial and doesn’t seem to work. there is no change in my dashboard. Is this compatible with every theme?

    • esteban says

      ‘Sidebar’,
      ‘before_widget’ => ”,
      ‘after_widget’ => ”,
      ‘before_title’ => ”,
      ‘after_title’ => ”,
      ));

      register_sidebar(array(
      ‘name’ => ‘Homepage’,
      ‘before_widget’ => ”,
      ‘after_widget’ => ”,
      ‘before_title’ => ”,
      ‘after_title’ => ”,
      ));

      register_sidebar(array(
      ‘name’ => ‘Homepage (Blog Style)’,
      ‘before_widget’ => ”,
      ‘after_widget’ => ”,
      ‘before_title’ => ”,
      ‘after_title’ => ”,
      ));

      register_sidebar(array(
      ‘name’ => ‘Footer Widget 1′,
      ‘before_widget’ => ”,
      ‘after_widget’ => ”,
      ‘before_title’ => ”,
      ‘after_title’ => ”,
      ));

      register_sidebar(array(
      ‘name’ => ‘Footer Widget 2′,
      ‘before_widget’ => ”,
      ‘after_widget’ => ”,
      ‘before_title’ => ”,
      ‘after_title’ => ”,
      ));

      register_sidebar(array(
      ‘name’ => ‘Footer Widget 3′,
      ‘before_widget’ => ”,
      ‘after_widget’ => ”,
      ‘before_title’ => ”,
      ‘after_title’ => ”,
      ));
      }

      // Add post thumbnail functionality
      add_theme_support(‘post-thumbnails’, array(‘post’));

      add_image_size(‘post-image’, 610, 250, true);
      add_image_size(‘featured-image’, 516, 340, true);
      add_image_size(‘featured-image-thumb’, 70, 60, true);
      add_image_size(‘widget-image’, 290, 160, true);
      add_image_size(‘widget-image-thumb’, 50, 50, true);
      add_image_size(‘media-thumb’, 140, 90, true);
      add_image_size(‘related-thumb’, 134, 90, true);

      // Include boostrap file for the pyre theme framework
      include_once(‘framework/bootstrap.php’);

      // Custom Functions
      include_once(‘framework/functions.php’);

      // Shortcodes
      include_once(‘shortcodes.php’);

      // Updates Notifier
      include_once(‘update-notifier.php’);

      // Profile Metaboxes
      include_once(‘framework/profile.php’);

      // Translation
      load_theme_textdomain(‘Avenue’, get_template_directory() . ‘/languages’);
      $locale = get_locale();
      $locale_file = TEMPLATEPATH . ‘/languages/’ . $locale . ‘.php’;
      if(is_readable($locale_file)) {
      require_once($locale_file);
      }

      // How comments are displayed
      function avenue_comment($comment, $args, $depth) {
      $GLOBALS['comment'] = $comment; ?>
      <li id=”comment-”>


      – ‘Reply’, ‘add_below’ => $add_below, ‘depth’ => $depth, ‘max_depth’ => $args['max_depth']))) ?>

      comment_approved == ’0′) : ?>

      /* IMPORTANT !
      * This function registers the same custom post type as the The Events Calendar plugin
      * http://wordpress.org/extend/plugins/the-events-calendar/
      * This also registers WordPress’ native categories and tags while associating them with the Events Calendar Plugin
      */
      add_action( ‘init’, ‘add_calendar_taxonomy’, 0 );
      function add_calendar_taxonomy() {
      register_post_type(‘tribe_events’,array( // Registers Events Calendar Custom Post Type
      ‘taxonomies’ => array(‘category’, ‘post_tag’) // This registers the native WordPress taxonomies with The Events Calendar
      ));
      }

      <?php }
      // Trim end of excerpt
      function pyre_trim_excerpt($text) {
      return rtrim($text, '[...]');
      }
      add_filter('get_the_excerpt', 'pyre_trim_excerpt');

      • Jonah says

        Hi Esteban,

        It should work with any theme. Make sure you have proper single quotes instead of apostrophes in the code block though.

        Regards,
        Jonah

  4. Mark says

    I’m curious about this technique.

    It seems little cleaner to me to use register_taxonomy_for_object_type rather than reregistering the post_type.

    I got this working with this code: http://pastebin.com/YQMniHYi

    The only big thing I had to do was give my action a low priority so it fired after the tribe_events post type was registered.

    I haven’t done too much testing, but is there a reason why the way in the post is better than this?

    • Jonah says

      Hi Mark,

      Nice suggestion. From a performance and general programming standpoint I could see why re-registering the post type might be unnecessary and less efficient than using register_taxonomy_for_object_type(). That seems to work fine but I don’t know enough about the internals of WordPress to say for sure which one is better. Good question though.

      Regards,
      Jonah

      • Steven says

        Mark – Great point on using register_taxonomy_for_object_type().
        Way to know your codex!

        Thanks for that info.
        - Steven

  5. Mark says

    Back with another question. I noticed in a Support Forum thread, Shane said that native tags will be added to the event post type in 2.1 (http://wordpress.org/support/topic/plugin-the-events-calendar-showing-the-events-on-the-home-page?replies=3#post-2476408).

    If we’re already adding tags to the post type, will anything bad happen with the upgrade?

    • Jonah says

      Hi Mark,

      I don’t think anything bad will happen but you’ll probably want to remove at least the post tags portion of the code since that won’t be needed any longer.

      Regards,
      Jonah

  6. Marion says

    I was finally able to show categories in events with this method, but I’m not able to show the event in my sliders. ¿Any ideas?

    • Jonah says

      Hi Marion,

      I would need some more specifics about your setup and some code samples to know why events are not showing in your sliders. Can you post some slider code so I can take a look? Or, even better, provide me WP admin/FTP access so I can look at things myself. You can email credentials to jonahcoyote [at] gmail [dot] com and I can take a quick look.

      Regards,
      Jonah

  7. Nick Froome says

    Did you spot the typo on line #9 of the code?

    ‘taxonomies’ =>

  8. Nick Froome says

    I have tried both approaches above. The checkboxes, etc, are there to edit Events. In both cases, flagging an Events with a Category used for Posts “works” (you can check the check box & save) but Events don’t show under a search of that category

    My Posts category for Events is “Events”. So, editing an Event I put it into category “Events” & save. I have Categories shown in the sidebar and it lists 16 Events. I click the link and it shows one – a Post. None of the Events is shown

    In many ways I preferred the approach of the earlier version of this plugin using Posts. It’s a more portable approach (I can move away from the plugin without lots of work) and my categories work.

    Also it seems as though Events don’t pop up via the Loop on the day they occur as they used to

    If I can have similar functionality to how it was before I’d be happy to spring for the Pro version

    • Rob La Gatta says

      Hey Nick. Are you saying you need some guidance here? While this isn’t yet a feature in PRO, it will be come 2.1. If you need something more immediate then, let me know and I can see if Jonah has any suggestions. No guarantees though :)

  9. Nick Froome says

    I’ve had a good look at various calendars and have realised how complex the topic is! The Post vs Custom Post debate is really about what you require for your site. I can see both approaches have merit

    The ideal scenario (most flexible) would be to have an event that could be a Post, an Event (Custom Post) or both, depending on requirement. There is a neat plugin that makes Posts published in the future visible now – that functionality, plus the ability to pop up in the loop, would make it the “Ultimate Calendar” – for some, at least

    I think the vital things are Post categories and tags. If that’s in Pro 2.1 I’ll be happy. If it’s doable now in 2.0.3, with your help, I am willing to try & feed back my experiences

  10. Nick Froome says

    Tried this again today. The Categories appear and Events can be tagged by category but a search for that category fails to return any events

    • Rob La Gatta says

      Thanks for confirming, Nick – let me see if Jonah has any ideas when he returns to support today. Stay tuned.

  11. Jonah says

    Hey Nick, do you have a URL I can look at and can you provide me some specific examples to look for in your setup? Just to clear, you’re saying that searching for a category name is not returning anything for events that are assigned to that category?

  12. Jason says

    I can confirm the behavior that Nick is seeing. I can assign events to Regular Post Categories, but the Events do not show on the Archive Pages for those Categories on the front end?

  13. Jonah says

    @Jason / @Nick – you guys will need to modify the query to include events in the search separately like so: https://gist.github.com/2007736

    Put that function in your functions.php file and you should be good to go.

  14. Jason says

    Hi Jonah,

    I added this to my functions.php, but when selecting a regular post category that an Event Post has been assigned to, the Event does not show up in that category archive listing?

    I’m also using the function to show Event Posts in the Main Loop as described here:
    http://tri.be/show-your-events-in-the-main-post-loop/

    In the Main Loop the posts are present and I can see that they are assigned to regular post category’s.
    One thing I did notice is that Events Posts that show up in the main loop on the home page do not list the “Event Categories” they are filed under, they only list the regular Post Categories they are assigned to? If they are assigned to no regular “Post Categories”, then they are listed as “Uncategorized”. Separate topic but one I’d also like to figure out.

  15. Terri says

    Hi All:

    I’m hoping that you can help: when I added the code to my child theme functions.php, then put the functions.php file at the root of the child theme (because that’s where it is in the parent theme), I went back to my dashboard and was dismayed to find that my site no longer worked when I clicked Events: I only saw a white page with the code itself. Needless to say I deleted the functions.php from the Child theme (whew!) and now I am back to square 0.

    Here’s the client production site:
    http://few.modernmediajapan.com/

    What I’m looking for is the way to both add regular post categories to events, and to therefore have all event posts automatically post to the slideshow and the front page.

    Sidenote: I really only need the ‘New’ category, because that’s what controls a posts’ appearance on the front page and slideshow but it’s fine if all of the categories are in Events Posts Categories.

    What should I do? Client presentation shortly: hopefully I just did something strange and it should work with my theme (which is from WPMU).

    thank you so much in advance,

    best,
    Terri

    • Rob La Gatta says

      Hey there Terri. Thanks for the note. What happens if you add this to the existing functions.php, without moving it anywhere? The change should work just by virtue of adding the code, but you wouldn’t have to create a new functions file or move the existing one anywhere.

      Let me know if I’ve misunderstood the issue here and I can try to troubleshoot from there.

  16. Terri says

    Hi Rob:

    Actually, it’s not working when I add it to the existing functions.php either: I get a blank page. I’m just copying and pasting the code and adding it just before the very last php symbol. Two questions:

    1. should I paste the entire functions.php code here so that you can see it and let me know what I’m messing up?

    2. if I’m able to get this change working, what happens when my parent theme is updated?

    Thanks again for your help.

    Terri

  17. Beej says

    Hmm, recieved this error:
    Parse error: syntax error, unexpected '=', expecting ')' in /xxx/xxx/functions.php on line 410
    when adding the aforementioned syntax to the end of the functions.php file for my theme. Line 410 would include'taxonomies' => array('category', 'post_tag') // Please advise =)

    • Beej says

      Disregard. Figured it out based on Mark’s aforementioned codex =)

  18. Pingback: Delicious Tags » biTs of M.E.

  19. Laura says

    We followed the tutorial, and we are able to select the “featured” (site-level) category when adding a new event, but the goal was to get the event image to be part of the homepage slider. Is that possible?

    http://www.actmissouri.org
    http://www.actmissouri.org/events/

    This is what was added to the functions.php. We’re using the Genesis framework (associate theme).

    Thanks!
    Laura

    • Rob La Gatta says

      Hi Laura. Thanks for the heads up here; we can definitely try and help you out, but do you mind bringing this discussion over to the forum (if you haven’t done so already)? It’s harder for us to provide support and give timely responses via these post comment areas – I’m disabling comments here momentarily as a result – but if you post a new thread we can absolutely take a look. Jonah is our resident Genesis expert so he should be able to help you out on this end.

      Thanks for your patience and for understanding!