How To Make a WordPress Events List

WordPress Events List

An events list is just what you would expect; a list of upcoming events, ordered by the date they are taking place on (Not the date they were written on!).

In this post, we are going to create our own events list that will let us add events simply by writing posts and assigning them to an “Events” category. It can’t get much easier than that!

This is exactly what we set up for a recent client of ours, American Majority. You can see their full events listing here, or solely the events in one state here (And on both pages, you’ll see the corresponding list in the sidebar as well).

There are a lot of calendar plugins out there, but there are two reasons this method may be better:

  • Full power of WordPress posts behind you, i.e. users can leave comments, and you can embed any kind of media you like in your post, even signup forms.
  • More usable for your readers. They can see event titles and descriptions instantly, and click the ones relevant to them. Calendars normally just show the date events are happening on (Because they have limited space in the sidebar), making you have to click on everything.

A bonus is that it never hurts to have one less plugin!

Where calendars shine is when they occupy a full page (i.e. They have room to give event titles!), and when you run a massive number of events (More than 1 each day, e.g. daily training session times for a golf coach etc.).

Create Some Events

The first thing we will do is to create some event posts for us to display later:

  • Create a new post, and give it whatever title and content you like.
  • Add the post to a new category called “Events” (Can be any name you like, but Events makes sense!)
  • In the custom fields section (Beneath the main content area), create a new custom field with a “Name” of “Date” (Without the quotes), and a “Value” of the date for the event, in the form: mm/dd/yyyy 00:00:00

Events Custom Field

  • Publish the post.

Setting the time is optional, but it would be useful if there are ever two events on the same day.

Build the Query

The great part part of this is that we can do all of the work with WordPress’ query_posts tag, so only relevant posts will be pulled from the database.

Let’s go through making the query, step by step.

1 – Grab Our Events Posts

First of all, let’s just pull in a certain number of posts from the Events category. In this case, we’ll take 5 (A nice number for a sidebar, but you could make it 20 or more in order to make a full events listing page).

<?php query_posts('showposts=5&category_name=events’); ?>

2 – Ensure the Posts Have a Date Set

As well as being in the right category, let’s make sure we’ve remembered to set the date of the event.

<?php query_posts('showposts=20&category_name=events&meta_key=Date); ?>

3 – Don’t Show Any Events Which Are Over

If the event date has already passed, we don’t want to list it anymore. We do this by getting the current date in PHP, and then comparing it with the dates in the posts.

<?php // Get today's date in the right format
$todaysDate = date('m/d/Y H:i:s');
?>
 
<?php query_posts('showposts=20&category_name=events&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate); ?>

4 – Order the Events So the Soonest is First

We do this by ordering the posts by their meta_value (i.e. the date), and setting the order mode to lowest first.

<?php // Get today's date in the right format
$todaysDate = date('m/d/Y H:i:s');
?>
 
<?php query_posts('showposts=20&category_name=events&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>

We now have our 5 posts, corresponding the next 5 events in the list.

Display the Events

We can use a regular WordPress loop now to format the posts in any way we like.

Sidebar Events List

For example, the following would display a list of the events with their excerpts beneath.

<ul>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
	<p><?php the_excerpt(); ?></p>
</li>
<?php endwhile; else: ?>
<li>Sorry, no upcoming events!</li>
<?php endif; ?>
</ul>
<?php wp_reset_query(); ?>

The last line puts our page query back to normal (i.e. the true content of the page), so we won’t break things further on!

(Optional) Organize Your Events

You may wish to organize your events into different groups. Thankfully, this is very easy to do. We just add a tag!

For example, on American Majority we split the events up by state, and then each state displayed only its own events (And a central page listed all of the events from every state).

State Events Only

Let’s say we tag a state with “Texas”, then on the Texas page, our new query would simply be:

<?php query_posts('showposts=20&category_name=events&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC&tag=texas'); ?>

All Done!

That’s you up and running now! And because we used the WordPress loop, what you can do with the posts is extremely flexible (e.g. Adding thumbnails, or publishing an RSS feed of them), the exact same way you would with any other set of posts.

Have you ever set up an events list before? How did you do it? With a plugin or a different method?

Share

  1. That’s quite probably the best way of doing it although it seems a bit of waste creating a whole load of posts and just using a custom field and title — you might be better building a widget or even just hardcoding it.

    • Michael Martin (1319 comments)18 January 10

      Hey Alex :)

      Thanks for the comment, not sure I follow you right though. What would you hard-code/do with the widget? Do you just mean letting the setup above work with WordPress widgets?

      Id love to hear any ideas that could make it better! :)

    • Essentially, I’m saying it’d be better to either just code into the sidebar the dates or you could build yourself a widget so you can easily and quickly update it. I’ve sent you an email – building widgets might make a good article :)

    • Michael Martin (1319 comments)19 January 10

      Ah, like an easier way of adding the dates in the Edit Posts page, instead of the custom fields interface? Sounds pretty cool! :)

  2. I am not that good with the code, is there any plugin that you think can do this automatically?

  3. Kristof (7 comments)18 January 10

    Thanks for the code – very useful.

    There is one small error though. You have the custom field labeled as “Date” but have meta_key=”Time” in the query. Needs to be the same for both to work. Since the events are based on date, I’d suggest just using “Date”.

    • Michael Martin (1319 comments)19 January 10

      Thanks very much for catching that Kristof! Going to fix it now! Sorry about that, pretty bad error to make!

  4. Dani (4 comments)18 January 10

    Thanks for this very useful tutorial!

    But I have one question… How would you separate events from normal posts? I think events should not appear in the main page, archives and rss feed.

    • Michael Martin (1319 comments)19 January 10

      Hey Dani,

      Another option along with Kristof’s would be to add add this to your functions.php file: (Just replace the 17 with the category ID of your events category :) ). That will keep it out of the feed, search results, and homepage.

      < ?php
      function eventsFilter($query) {
      if ($query->is_feed || $query->is_home || $query->is_search) {
      $query->set(‘cat’,'-17′);
      }
      return $query;
      }
      add_filter(‘pre_get_posts’,'eventsFilter’);
      ?>

    • Kristof (7 comments)19 January 10

      I think adding the code to the functions.php file to exclude the category is a better solution :)

      I suggested a custom category template because although the “Sorry, no upcoming events!” makes sense for an events category (if no events are found), it doesn’t make sense for other categories that generally display some sort of “Page not found” message (and visa-versa)

      To make this universal, perhaps modifying the ‘else’ statement to display specific message for the events category if no events are found and an alternate “Page not found message for all others?

    • RichM (1 comments)31 January 10

      This approach is just what I was looking for, so I’m thrilled I found it. I’ve gotten the category code working, but am having trouble with the edit to the functions.php file to exclude the events post from the index and feed. Whenever I edit the file, I keep getting errors. I edit Wordpress templates often, but don’t ordinarily tinker with the functions.php file. Is there a particular place this needs to go, or can I append this to the end of the file?

  5. Kristof (7 comments)18 January 10

    @Dani

    You can simply make an event specific category template “category-events,php” that uses the code. If you’re using WP 2.8 or below, you may have to name the template with the corresponding category ID instead of the name.

  6. Thanks Michael. This is a great way to handle something that many companies and organizations really need on their site.

  7. Dani (4 comments)18 January 10

    @Kristof

    How creating a category template will make the events disappear from the main page or the rss feed?

  8. BigM75 (1 comments)18 January 10

    thats great, i will make this on my sportspage

  9. Nice tutorial, but why not just use the post’s own date/timestamp for the event’s date and time? In your query_posts, just use the parameter ‘post_status=future’ to list all future events. Once the date/time has past it will not show up in the results. No need for custom values.

    • Kristof (7 comments)19 January 10

      Using the post time stamp sounds like a good idea but setting a future date also means that the post isn’t available to display (it’s technically not published until that date). Using the custom field allows the post/event to be viewable now and then not display when the date is past

    • Right, that’s why I would add the ‘post_status=future’ to the query_posts function so that posts with a future date will show up…

    • Kristof (7 comments)19 January 10

      I understand how using the ‘post_status=future’ could list the events on the category page but, since the event/post isn’t published, wouldn’t that prevent a user from clicking through to view the full post/details?

    • Sorry–my bad. Yes, you’d run into an issue if you linked a future post to single.php for display (assuming a visitor is not logged in). You’d have to add this code to your functions.php in order to display it for people not logged in:

      function show_future_posts($posts) {
      global $wp_query, $wpdb;
      if(is_single() && $wp_query->post_count == 0) {
      $posts = $wpdb->get_results($wp_query->request);
      }
      return $posts;
      }
      add_filter(‘the_posts’, ’show_future_posts’);

    • Kristof (7 comments)19 January 10

      @Jeff -

      That’s a nice solution for using the future post date for this. But doesn’t adding the code to functions.php also mean it would essentially remove the benefit of being able to schedule posts for future dates? I guess it’s up to each site author on what works best for their needs. If they’re not scheduling posts then you’re solution is a nice way to use functions already included in WP. Either way, there are now two options for people to select from. Thanks.

    • Good question. :-) Not sure on that; I suppose the function could be modified to only affect those posts of a certain category (e.g. events). But I haven’t gotten that far…

    • Michael Martin (1319 comments)19 January 10

      Hi Jeff,

      That’s an interesting way of doing things. I think that like Kristof said, there would be a few complications in setting it up (Another one being that at the minute, we can take the Events category feed and use that to publicize events. Would need to adapt that as well to work with future posts).

      It’s a route I hadn’t thought of at all though, very clever! :)

      I think that in the end though, after it’s all set up, the two methods are similar enough for it not to be an issue. Neither one is much more complicated than the other when it comes to editing the posts.

  10. redwall_hp (139 comments)19 January 10

    I have a site where I’ve been maintaining an event list by hand for some time. I considered something similar to this, but I didn’t want to pollute my Posts table with a bunch of tiny event items. :)

    I still haven’t decided on a solution; I have a couple of plugins to try, and I had experimented with the Pods plugin. There’s an event calendar plugin, called “Events Manager” that might do it though.

    • Michael Martin (1319 comments)19 January 10

      Hey Matt,

      Good point. The thing about Events Manager though is that it will still be creating database entries and querying them. Would be interested in seeing what the actual performance difference was :)

      If you want to avoid the database altogether, I suppose you could embed a Google Calendar?

    • redwall_hp (139 comments)19 January 10

      The difference is that it stores them in its own table. I have a weird phobia about putting things like event entries into the main post table. It just seems like Bad Things could happen. :)

  11. That’s great,Using the post time stamp sounds like a good idea but setting a future date also means that the post isn’t available to display. I considered something similar to this, but I didn’t want to pollute my Posts table with a bunch of tiny event items.

  12. Great…this tutorial increase my knowledge. Because usually for events in wordpress .. I use the calendar.
    Thanks…

    • Michael Martin (1319 comments)19 January 10

      Welcome Eko! I think that would be the default for a lot of people, but simple list can be a better alternative quite often :)

  13. Hi Martin, sorry for contacting you here. I’ve sent you several emails since last month but didn’t get your response yet. Is there any way that I can reach you? :)

    Thanks.

    Regards,
    Lee

    • Michael Martin (1319 comments)19 January 10

      Since last month?? Yikes!

      My inbox is fairly backed up, mostly down to writing for PBD posts now. A lot of people in need of a response :(

      I’ll go and find yours now and reply, sorry!

  14. Very cool post, and very interesting use of WP. Well done, Michael!

  15. Oh hells yeah!

    I’m gonna try my hand at putting this on my blog. Lot more events have been coming up so this would be a nice touch!

  16. I like to use google calendar api.

    It is more difficult to use, but if you have a good java script knowlege it wont be a problem.

    Thanks

  17. Great tutorial thanks I was looking for something like this for wordpress.

  18. Michael (18 comments)20 January 10

    Wow! Exactly what I was looking for! Works great, thank you.

  19. Nice tut, I just put together something very similar for a client. Excluding the events from the blog (and RSS) makes for a nifty events list.

    Add in a custom meta box for easy selection of month/day/year/time and it gets even niftier!

    • Michael Martin (1319 comments)21 January 10

      That’s a good point Adam, the custom meta box for inserting the dates definitely would be a handy addition! Thanks for the comment :)

  20. Loren (1 comments)21 January 10

    Terrific tutorial. I’m trying to get this working using the Thesis theme and can’t find the right file(s) to add the new code. Anyone have a suggestion on what I can/should try?

    • Michael Martin (1319 comments)21 January 10

      With Thesis, you’d need to add the code to your custom functions.php file, and then use a Thesis hook to put the code into the right place.

      Trust me, that sounds much more difficult than it actually is!

      Your best bet would be to look on the Thesis site for their tutorials on customizing the theme, and posting on the support forum about it (Just link to this tutorial and your site, and tell them where you’d like to put the events list on your site. They should be able to give you the exact instructions no bother :) )

  21. Arfandia (2 comments)22 January 10

    awesome tutorial.. i’ve to try this at my blog.. thanks

  22. Iz (5 comments)22 January 10

    Hi Michael,

    Great tutorial! This is just what I have been looking for! My only issue is, along with being a WP novice, is not knowing where to put this code into. I am currently using the pyrmont-v2 theme.

    Any help would be appreciated!

    Thanks.

    • Michael Martin (1319 comments)23 January 10

      Sorry Iz, I’ve never used the theme you mentioned there. Do they offer any sort of support section you could ask for help it?

    • Iz (5 comments)25 January 10

      No, the designer doesn’t offer support.

    • Michael Martin (1319 comments)26 January 10

      Ah, that’s a shame. If it’s a free theme, I can understand (Even problems that only take 10 minutes to sort out really build up once a few people write in :( ).

      You might be worth investing in a premium theme, which guarantees to support you?

      Alternatively, you could hire someone (just link to this page and tell them you’d like them to set it up for you, it really shouldn’t take them long. I’ve laid everything out as clear as I can :) )

    • Iz (5 comments)28 January 10

      Do you have any idea into which template it would most likely go into?

      I’ve got the following:

      404.php
      comments.php
      footer.php
      header.php
      index.php
      page.php
      searchform.php
      search.php
      sidebar.php
      single.php
      functions.php
      archives.php
      links.php

      Thanks.

    • Iz (5 comments)10 February 10

      Thanks for the reply, Michael. I will give sidebar.php a try and see how it goes.

    • Iz (5 comments)18 February 10

      Well, I tried and it doesn’t work for me, I’m sure I’m doing something wrong.:(

  23. redwall_hp (139 comments)22 January 10

    I just realized that this might work really well if you used WP 2.9’s custom post type API instead of categories. I may have to look into it further and give it a try.

    • Michael Martin (1319 comments)23 January 10

      Would be very curious to see how it works out! I don’t think it would much of a difference in the front-end, but might be even cleaner in the back. Nice idea :)

  24. Kimber (3 comments)23 January 10

    Good tutorial. I’ve been looking for something like this for a while. Something that I don’t think has been addressed is the SEO benefit of using this method. Creating events like this and using an SEO plugin can help grow the amount of pages/content on your site in the eyes of engines. If you’re looking to increase rankings for events or event related content, this is a great way to accomplish both.

    I’m working on a site – lebanonillinois.org and have an events section. I’ve been looking for a number of different methods for implementation. Using This as well as adding the function of an excerpt for a brief description, I can probably roll my current event calendar into something much better….

  25. Martin (5 comments)23 January 10

    Very nice approach on building up a feature a lot of people (me included) requested…
    I wondered if your date(m/d/Y H:i:s) is the right choice, because it would call 23/12/2009 later than 01/02/2010 wouldn’t it? I think the order should be like Year-Month-Day-Hour-Minute…

    One thing I really recommend is the use of the great plugin “Custom Field Template”, which enables you to create custom metaboxes in admin area with all kinds of form elements like a JS-datePicker… works like a charm :D

    • Michael Martin (1319 comments)26 January 10

      You could well be right about that date. I’m not sure how the meta compare works exactly, but testing it out on the site I mentioned, it worked perfectly for events spanning two months, but haven’t test on two years.

      I’ll have to try that plugin though, a handier way of inputting the dates really is the one thing the post is missing!

    • You are correct. The sort will treat the values as text so the best way to accommodate for this is to use this format (yyyy/mm/dd). I use a similar technique as described in this post and do like it much more than event/calendar plugins. Fun stuff!

  26. I have a couple of sites that I’m definitely going to try this out on. I was actually just checking out event/calendar plugins, but I this solution will definitely work a lot cleaner. Good job on this tutorial.

    • Michael Martin (1319 comments)26 January 10

      Thanks Adam, great to hear you enjoyed it! Feel free to share the site links when you’re done, would love to see it in action!

  27. Clive (3 comments)25 January 10

    Really like the sound of this.

    A problem I’m going to have is I need to present a list of months (say the next five months), and visitors click on a month and are shown a list of events taking place within those months. To complicate things, the events might last ten weeks, which means they need to appear in under two or three months!

    How easy would it be for me to tweak this code to do what I’m after?

    • Michael Martin (1319 comments)26 January 10

      There are two ways that I can see. The complicated one that might well work, though I’ve never tried it, would be fiddling with the &meta_compare to see if you can get it to take 2 comparisons (i.e. Finds posts that are >= month day 1, and < = month day 30). I've never tried that with WordPress though, so I really don't know if it's even possible.

      The handier solution would be to just tag your events with the month they're in. Then, for example, for February, you'd just be using the query line from the very end of the post, e.g.

      =&meta_value=’ . $todaysDate . ‘&orderby=meta_value&order=ASC&tag=February’); ?>

      You don’t even need to worry about issues with showing months from various years, the whole future event section will take care of that for you.

      And it solves the multiple-month issue nicely, just tag those longer events with as many months as they need!

      What do you think?

    • Clive (3 comments)31 January 10

      Hmm. Had problems using the meta_compare because it’s treated as a string. My original plan was the categories for each month, but then in the admin panel the categories weren’t displaying in the right order and I didn’t like that.

      Finally, I think I’m on it. I’m running the loop as normal with a category called Events, and then running a script to filter the relevant months after fetching the meta_data like this:

      $beginDate= explode(“/”,get_post_meta($post->ID, “Begins”, true));

      It gets me around the string problem then all I need to do is turn the numbers around (I’m using British format DD/MM/YYYY) before running this:

      function get_months($beginDate, $endDate) {
      $time1 = strtotime($beginDate);
      $time2 = strtotime($endDate);
      $my = date(‘mY’, $time2);

      $months = array(date(‘F’, $time1));

      while($time1 < $time2) {
      $time1 = strtotime(date('Y-m-d', $time1).' +1 month');
      if(date('mY', $time1) != $my && ($time1 < $time2))
      $months[] = date('F', $time1);
      }

      $months[] = date('F', $time2);
      return $months;
      }

    • Using tags like Michael suggested is the best way to handle events that have date ranges spanning multiple months.

    • Clive (3 comments)15 February 10

      I beg to differ, Josh. For me, categories was not the solution. As I was building this for a client, having a list of months which weren’t in alphabetical order was not the way to go.

      The method described above uses two custom fields called Begin and End. Easier for the client – a time-saver for me (in the long-term).

  28. Um, you just saved me a huge amount of time with this fantastic solution for displaying a list of upcoming events in Wordpress. I owe you a big thanks for sharing.

  29. Great post. I never done making a event list to my word press blog but I think I will try that one soon.

  30. Bill (4 comments)25 February 10

    Love this solution, but am having a couple problems implementing it. First, it isn’t sorting my events by the custom field, but rather by original post date still. Second, instead of displaying an excerpt, I am trying to get it to display the date of the event under the link.

    Any help would be appreciated.

Leave a Comment

Your reply will be added to the comment above (Below any other replies to this comment) -

(We DoFollow)

Not sure how to get an image with your comment?