All-In-One Event Calendar events in blog home, categories and tags

This starts when I found what seemed like just the right WordPress event calendar plugin, all-in-one Event-calendar, with lots of good features, useful shortcodes etc. However, like many event plugins, it uses a custom post type, and normally will not show up in blog postings. My client wants scheduled events to show up in the blog home, and lists by category or tag as well. Say, clicking “Live Music” should bring up videos and photo albums of past performances and scheduled future event info.  Finding out how to add the custom post type was not too hard, but the events use their own custom “taxonomy” as well, their own kind of category and tag that aren’t really the category and tag of regular posts. I really wanted to solve this problem, not just for this specific plugin, but for any custom post types and taxonomies I run into in the future as well. This has led me through some murky sketchily documented regions of the WordPress world around custom taxonomies and query modification, to at last arrive at this piece of code that can be placed in any theme’s functions.php file, and will cause ai1ec_event type custom posts to be displayed in blog home, feeds, and lists by category or tag. This uses the very handy pre_get_posts action hook which can change which posts are displayed without having to change anything else about the blog, thus keeping the code nice and portable. This can be modified to query across any set of custom post types or taxonomies.

// Add this to your theme's functions.php
function edit_my_query($query) {
  // Modify category and tag listings to include ai1ec events and all uses of the same term
  //  across event and post taxonomies
  //  ie live-music or arts whether they are event or post categories
  // also include ai1ec events in blog home and feeds
  if ( ( is_home() || is_feed() || is_category() || is_tag() ) 
          &&  empty( $query->query_vars['suppress_filters'] ) ) {
    // The 'suppress_filters' test above keeps your menus from breaking
    $post_type = get_query_var('post_type');
    if($post_type && $post_type[0] != 'post') {
      $post_type = $post_type;
    } else {
      $post_type = array('post','ai1ec_event'); // add custom post types here
    }
    $query->set('post_type',$post_type);
    if (is_category() || is_tag()) {
    // Add custom taxonomies to category and tag pages
    if (is_category()) {
        $taxonomy1 = 'category';
        $taxonomy2 = 'events_categories';
      }
      if (is_tag()){
        $taxonomy1 = 'post_tag';
        $taxonomy2 = 'events_tags';
      }
      $queried_object = $query->get_queried_object();
      $slug = $queried_object->slug;
      $query->set('tax_query', array(
        'relation' => 'OR',
        array(
          'taxonomy' => $taxonomy1,  'field' => 'slug', 'terms' => $slug
        ),
        array(
          'taxonomy' => $taxonomy2, 'field' => 'slug', 'terms' => $slug
        )
      ));
    }
  }
}
add_action('pre_get_posts', 'edit_my_query');

I believe this is just the tip of the iceberg for WordPress multiple taxonomy aggregation and taxonomy mapping that will start to occur as custom post types and taxonomies continue to multiply like free plugins and spread through the WordPress ecosystem. We who wish to control what material site visitors and subscribers will have promoted to them regardless of what WordPress has it filed under would like to be able to invisibly and seamlessly  assemble that material behind the scenes.

The existing custom post type and taxonomy plugins for WordPress seem to emphasize creating and managing your own set of custom posts, tags and fields, but there seems to be little for managing and presenting the exploding array of custom types being introduced as more and more plugins take advantage of custom post types and taxonomies.  A plugin to generate functionality like the above from checkboxes for every post type and taxonomy in the database and a menu of page types would be a handy thing. Plugins that depend on a visitor to navigate taxonomies in a sidebar widget are a waste of time. There’s no reason for a site visitor to think anything more complicated than clicking a menu or category is going on, and this functionality is too useful to restrict to a widget.

This entry was posted in WordPress Nerd Info and tagged , , , . Bookmark the permalink.

22 Responses to All-In-One Event Calendar events in blog home, categories and tags

  1. Rasmus says:

    Thank you, this was very helpful!

    I have two questions:
    First, I’m unsure how the category-thing should work. On my website, when I click on a post category when one also exists as event category, then only the regular posts show up. I thought it was meant to show both posts types, right?

    Second, I was hoping you might be able to help me with something related. I use the Mailpress plugin to send Newsletters, but I can’t seem to get it inlcude the ai1ec_event events (which is horrible!). It only includes the regular posts. If you know the plugin, do you have a suggested solution? Otherwise, do you have an idea abiut how one could “mask” ai1ec_event posts as post posts?

    Again, thank you for the great piece of code above. Now the ai1ec_event posts show up on my front page!

  2. juanb says:

    saved my life, mate. THANKS A MILLION

  3. Gary Meyer says:

    This solution worked great for me. Thank you!

  4. Mike says:

    Yes, it works! Great! Tha category thing: you have (post) category named and (events) category named THE SAME, it shows events and posts on category.php.

    BUT – it works this way: if the event is in subcategory, it is not showed in parent category posts listing. How to do this?

  5. Gary Smith says:

    Hi,

    This may be the solution I’m looking for so that when a member posts an event, it appears on my front page like a regular blog post.

    If so, am I correct in thinking that I need to add the different AIOEC event types into this line $post_type = array(‘post’,’ai1ec_event’);

    Do I need to add or change anyting else?

    Thanks

  6. dudu says:

    Hello, you said the all-in-one calendar as shortcodes, but I found no documentation about this. Do you have any link?

    thank you
    dudu

  7. John says:

    I dropped this into my functions.php file just before the last “?>”, and then got an error trying to reload the home page – it was a vague error – not configured properly or something like that (using the Thematic Theme, btw). Was i supposed to just add this code, or replace it someplace? Here’s how it lays in the edited functions.php file

    Many thanks!!

    john

    function wp_allowed_protocols() {
    static $protocols;

    if ( empty( $protocols ) ) {
    $protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn' );
    $protocols = apply_filters( 'kses_allowed_protocols', $protocols );
    }

    return $protocols;
    }
    // Add this to your theme's functions.php
    function edit_my_query($query) {
    // Modify category and tag listings to include ai1ec events and all uses of the same term
    // across event and post taxonomies
    // ie live-music or arts whether they are event or post categories
    // also include ai1ec events in blog home and feeds
    if ( ( is_home() || is_feed() || is_category() || is_tag() )
    && empty( $query->query_vars['suppress_filters'] ) ) {
    // The 'suppress_filters' test above keeps your menus from breaking
    $post_type = get_query_var('post_type');
    if($post_type && $post_type[0] != 'post') {
    $post_type = $post_type;
    } else {
    $post_type = array('post','ai1ec_event'); // add custom post types here
    }
    $query->set('post_type',$post_type);
    if (is_category() || is_tag()) {
    // Add custom taxonomies to category and tag pages
    if (is_category()) {
    $taxonomy1 = 'category';
    $taxonomy2 = 'events_categories';
    }
    if (is_tag()){
    $taxonomy1 = 'post_tag';
    $taxonomy2 = 'events_tags';
    }
    $queried_object = $query->get_queried_object();
    $slug = $queried_object->slug;
    $query->set('tax_query', array(
    'relation' => 'OR',
    array(
    'taxonomy' => $taxonomy1, 'field' => 'slug', 'terms' => $slug
    ),
    array(
    'taxonomy' => $taxonomy2, 'field' => 'slug', 'terms' => $slug
    )
    ));
    }
    }
    }
    add_action('pre_get_posts', 'edit_my_query');

    ?>

  8. Gary meyer says:

    Is there anyway to list the post pages in event date order? And not display past events?

  9. Vidal says:

    It is working nice http://www.racecountdown.com/
    But I was wondering if it is possible the have the events show up in reversed order.
    First upcoming events.

    thanks

  10. Dennis says:

    I am attempting to use the ai1ec plugin with the blog-in-blog plugin to create a feed of upcoming events. The blog-in-blog plugin will retrieve the correct data when I query a standard post field, but when an event field is queried, I recieve an error. Can anyone assist me with this issue?

  11. Doris says:

    Thanks! This worked perfectly!

  12. Aahz says:

    Ian,

    You’re a brilliant man and this is a great and useful post! So, I’m hoping I can tap into that brilliance to solve another All-In-One Event Calendar problem I am having…

    Since Ai1EC can’t send events to FB Pages I’m using the leenk.me service to post new events to my site’s FB page.

    My problem is that when leenk.me pushes an event it uses the the post number format (http://porccalendar.com/?p=1481) which returns a 404 because Ai1EC uses a more descriptive format (http://porccalendar.com/?ai1ec_event=test-do-not-visit&instance_id=2).

    Any way to to sync these two urls up automatically? Or to get a new event in Ai1EC to also create a post on the post id it creates?

    -Aahz

  13. you are awesome!! you saved my life!!

  14. prokopis says:

    I have the following part of code

    This code is put the NORMAL categories in an array to select then in an option page in my theme.

    // Pull all the categories into an array
    $options_categories = array();
    $options_categories_obj = get_categories();
    foreach ($options_categories_obj as $category) {
    $options_categories[$category->cat_ID] = $category->cat_name;
    }
    // Pull all the pages into an array
    $options_pages = array();
    $options_pages_obj = get_pages(‘sort_column=post_parent,menu_order’);
    $options_pages[”] = ‘Select a page:’;
    foreach ($options_pages_obj as $page) {
    $options_pages[$page->ID] = $page->post_title;
    }

    My problem is that I cant to make it filtering AND the all in one event categories with the normal categories, in the same list, OR, to make it select ONLY the all in one event categories in the array.

    Can any any body help me to do this? I have not the knowlenge to do this. I dont know is what i am asking for is hard or not, but i just cant do it myself.

    thank you for your time and for your understanding.

  15. Colin says:

    I dropped in the code as suggested and while it worked fine I have a header called “Our Mission” that the content of it disappeared. I removed the code and it showed up again. I am using Genesis Enterprise Template.

    Thoughts?

  16. narendra says:

    Is there any way to make category field required.

  17. Hanna says:

    It worked!

    Thank you, I’m a php-newbie and would have never figured this out by myself. Very helpful!

  18. spine says:

    Hi!
    Categories and tags are new to me. I just built a website : http://www.myonevision.com a job listing portal………..on jobpress theme…..I just wanted to disable the post categories and assign them to my job categories…how can i do this, Please help?

  19. Stuart says:

    Thank you so much for this code, it works perfectly – it should be basic functionality.

  20. Antoine says:

    Hi Diana!

    You’re a life saver! Thank you very much that worked great!

    I do have one question though. The theme that I’m using has a built in slider for recent posts. And even though my event from all-in-one calendar is showing up as a post on my blog, it’s not showing up on the slider. Do you have a trick for that by any chance?

    Thank you!

  21. Jacq says:

    Thank you SO much. I can now using your code create a category with the exact same name as my event category and pull this to a page with my order changed! I have spent DAYS working through this, this is fantastic.

Leave a Reply to Colin Cancel reply

Your email address will not be published. Required fields are marked *