Show Your Events in the Main Post Loop

Published on: October 21, 2011 | Categories: Tutorial

A lot of people have asked how to get their events to show in the main post loop now that the open source plugin has also moved to a custom post type.

The good news is that John, with a little inspiration from Justin Tadlock, just emailed me the basis for a solution. Looks like it needed a bit of work and I am updating it with a solution from Jonas that was posted at .org. I haven’t had a chance to test it so we may need to tweak this as a community, but I’m sure there are plenty of you who will help me get it figured out!

Just add this to your theme’s functions.php to include events on the home page.

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_archive() || is_tag() || is_home() && empty( $query->query_vars['suppress_filters'] ) ) {
    $post_type = get_query_var('post_type');
	if($post_type)
	    $post_type = $post_type;
	else
	    $post_type = array('post','tribe_events','attachment');
    $query->set('post_type',$post_type);
	return $query;
    }
}

As you can imagine, it will show events in the post loop in the order they were added, not based upon the date of the event. Hope this is useful.

Have a wonderful weekend.

16 Responses to Show Your Events in the Main Post Loop

  1. mike says

    if you use this then the event link no longer shows the calendar

    • Shane Pearlman says

      Good catch! Ok, we’ll fiddle with it a bit more.

  2. BILL GRAM-REEFER says

    It would be helpful to have a toggle in the regular EDIT POST window to be able to select whether a POST that contains event info like OCCUPY PROTESTERS TARGET YOUR TOWN, NOV 5, can appear as a POST but also appear in Calendar and widgets as event. For some posts, not all. Otherwise, for a news site that comments on upcoming events, one must maker two separate postings, one in regular loop, and one as an event.

    Correct me if I’m wrong, but aren’t custom posts types selectable in the EDIT POST WINDOW (aside, gallery, link, etc)

  3. John Nasta says

    These are the things I have noticed so far with 2.0.1…

    1) When using the above code, as Mike says the calendar no longer works. This is a bit of an issue because most of the posts on the site are events and the homepage is spotty and has mostly old posts on it without the events there.

    2) When using the above code in functions.php, php the_category(‘, ‘) displays links on the home page to regular post categories but not event categories. Is there a code that will display both post categories and event categories, and/or is that another function that has to be added?

    3) The Google link displays a nice local map but the Google map is way zoomed out

    4) If I set a Featured Image in the post, it appears below the Google map full-size, and then again in the post with the size and placement that I have specified.

    Thanks!

    • John Nasta says

      p.s. It would be nice of your comments template on this site showed the date & time of the comments so we would know how recent they are. Thanks!

  4. John Nasta says

    For the issue of events showing on the home page (and archive and tag pages) I posted some code here that seems to work…

    http://wordpress.org/support/topic/plugin-the-events-calendar-showing-the-events-on-the-home-page?replies=3#post-2426195

  5. John Nasta says

    Very clever of you to notice that I copied it from Justin Tadlock. I know enough about php code to know what to copy. Also good that you changed where I had originally posted is_category() to is_archive(). That will be more useful to people.

    Thanks for the great plugin!

    John (a.k.a. Jonas)

  6. Jason Olshefsky says

    I’d like to do something similar: I’m updating my website to use an online database so to keep the format, I’m looking to make a custom loop that displays events in order. I’ve already done something similar to pull blog posts in chronological order. I basically made two filter functions that work like this:

    function show_events ( $from, $to )
    {
    global $g_from;
    global $g_to;

    $g_from=$from;
    $g_to=$to;

    function this_week_where ( $where )
    {
    global $g_from;
    global $g_to;
    if ( $where > ” ) $where = $where . ‘ AND ‘;
    $where = $where . ‘( post_date > “‘ . $g_from . ‘ 00:00:00″ AND post_date

    <?php the_date('l, F jS, Y', '’, ”); ?>

    <a href="”>
    by at ()

    What I’d need to know, then, is rather than post_date, is there an accessible equivalent field (table?) like event_start_date?

    • Jason Olshefsky says

      Frighteningly, that didn’t display right because it appears bto be EXECUTING PHP CODE. Here’s it without the php tags munged …

      function show_events ( $from, $to )
      {
      global $g_from;
      global $g_to;

      $g_from=$from;
      $g_to=$to;

      function this_week_where ( $where )
      {
      global $g_from;
      global $g_to;
      if ( $where > ” ) $where = $where . ‘ AND ‘;
      $where = $where . ‘( post_date > “‘ . $g_from . ‘ 00:00:00″ AND post_date <= "' . $g_to . ' 00:00:00" )';

      return $where;
      }

      function date_ascending_order ( $orderby )
      {
      return 'post_date ASC';
      }

      add_filter('posts_where', 'this_week_where' );
      add_filter('posts_orderby', 'date_ascending_order' );

      query_posts('posts_per_page=-1');


      …}

      • Peter Chester says

        Hi Jason. Our comments are NOT executing PHP. Actually they strip it out. to display php code in your comment use the <code> tags.

        < ?php echo 'This is wrapped in tags'; ?>

  7. Orlando says

    Hi, I added the script and work as refered… but the events aren’t sent trough the rss feeds. What can be done to correct this? Using with theme: Arras 1.5.1

  8. Ryan says

    Hi – thanks all for the work on this code. When I use this code, indeed the events show up as posts and I am able to use them in the slider of my theme. However, there is an unintended effect – I lose the featured page on my homepage.

    Why would this code affect a page?

    • Jonah says

      Hi Ryan, hard to say but I’m guessing it could have something to do with there being another custom post type involved for featured items? If so, you could try adding that post to the array along side the others like so:

      $post_type = array(‘post’,'tribe_events’,'attachment’,'featured_post_type’);

      ….replace ‘featured_post_type’ with whatever the actual post type is.

  9. Facter says

    So is the code in the top post the actual correct code? Im noting a few additions and changes in the comments – has that piece been updated to reflect any fixes etc? ie the event link not chwoing the calendar? Sorry, just want to make sure the code is correct before I do the massive changeover to using this plugin for our events.

  10. Facter says

    .. also, if you use this, how does it affect categories? If I post an event to a mirrored category (that is the site category) will it map to the site category automatically? Such a pita that we have to re-create the whole category structure for the event calendar .. but I dont want two different taxonomies and am just wondering how the two will integrate.