How To Create An Events List With WordPress

When I did the recent design changes to DavidRisley.com, one of the things I wanted to do was add an events list. You can see it off in the right sidebar.

The idea behind it is simply to be able to use the blog as a central platform for all events that I do, including webinars, launches, live events – anything with a particular date assigned to it. Even if I do a call only for my students, I’ll still list it here on the blog.

From a technical perspective, this looks easy. But, it isn’t. It took me some time and testing to figure out what I’m about to show you.

My goals were simple:

  • List upcoming events.
  • Have each event be clickable into a post about that event.
  • Once the event is past, don’t display it.

First, I was going down the path of displaying future posts. This is easy to do with WordPress. I’d simply timestamp the post with the time of the event and future post it. Right? Wrong. That approach would work if I don’t want anybody to be able to click for more details, however WordPress prohibits people from reading future posts unless you’ve got sufficient privileges. So, scratch that.

Next approach is to use regular posts, assigned to an “Events” category, and display those. Again, this doesn’t serve my needs because it would, by default, not show upcoming events, but only past due ones.

Next approach is to use regular posts, but use a custom field for the date of the event. I was getting warmer by this point, however the problem remains – past events are still listed.

Let’s jump right to the solution. We want to query for posts using custom fields, and make the query so that when that custom field is past a certain value (the present date), it doesn’t display. After tons of trial and error, I arrived at this:

<h1>Upcoming Events</h1>
<?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' => 'events',
'orderby' => 'meta_value',
'meta_key' => 'eventsortdate',
'meta_compare' => '>=',
'meta_value' => $timecutoff,
'order' => 'DESC'
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$eventdate = get_post_meta($post->ID, "eventdate", true);
?>
<ul id="events">
<li>
<strong><?php echo $eventdate; ?></strong><br />
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
</ul>
<?php endwhile; else: ?>
<ul id="events">
<li><?php _e('No Events Scheduled! Stay Tuned.'); ?></li>
</ul>
<?php endif; ?>

What I’ve done is create TWO custom fields: “eventdate” and “eventsortdate”. The “eventdate” is the human-readable date and time of the event. The “eventsortdate” is the date of the event in YYYY-MM-DD format. By comparing the value of “eventsortdate” to the present date ($timecutoff), I can simply make the query ignore events where the day of the event is in the past. If it is greater than or equal to the present, it displays in all it’s glory.

Edit Post ‹ David Risley dot com — WordPress

When there are events on the list, it’ll look like this:

Make Money Blogging, Marketing, Blog Tips, Social Media - David Risley

The post link will be fully clickable so that a person can read details about the event as a regular post.

Hope this helps somebody. :)

Free eBook!

Like what you read?

If so, please join over 12,000 people who receive exclusive online business and blogging tips, and get a FREE COPY of my eBook, Six Figure Blogger Blueprint (PDF and MP3)! Just enter your name and email below:

  • http://richwallace.net/about Rich Wallace

    Impressive stuff, I could definitely use this for a client that I'm building a site for. Thanks for sharing this!

  • rfiil

    Good time of day! Very useful script, Will it be possible to implement on the platform Dle

  • http://www.talktorbay.com TalkTorbay

    This is exactly what I need. But I dont get it, where do I put the script? Sorry if thats a stupid question im new to wordpress! I put it in a text widget, but that didnt work.

  • http://www.darrensingleton.co.uk Darren Singleton

    Thanks for sharing this code David. You just keep giving back to the community!
    Thanks again,
    Darren

  • http://abundancehighway.com SuzieCheel

    David, That is such a net idea, thank you, has lots of potential

  • http://www.talktorbay.com TalkTorbay

    I put it in the post aswell, still doesn't work…anyone shed any light? Where do I put the code to make it work?

  • http://www.talktorbay.com TalkTorbay

    hello???? are you replying to this post????

  • http://www.talktorbay.com TalkTorbay

    incase anyone is reading this. It will only work if you install a text widget that will read php……I got it to work by installing the plugin w3 'devil inphp'.

    Thanks for the code david, but you really should reply to questions on your posts!!!! Especially in cases like this.

  • http://twitter.com/rosaleenortiz Rosaleen Ortiz

    Hey there.
    Not sure if you figured this out, but just in cases:

    Just put the script on a .php file (using a text editor like TextMate or Dreamweaver) and name it “category_xx.php”

    “xx” is the category ID for your “events” category (http://www.wprecipes.com/how-to-find-wordpress-category-id)

    Then you have to upload the file to your server (with Fetch, for example). Once it’s up you should be able to make any adjustment to it by going to the “Editor” section of the Appearances tab of your Dashboard.

    Thanks David for this tutorial, I’m going to give it a go.

  • http://twitter.com/rosaleenortiz Rosaleen Ortiz

    Ok, David, you are my hero. I was able to use this code for my Events page, for both Upcoming and Past Events. Love YOU!!!

  • http://www.addicottweb.com/ Hirsch

    This is a great solution, thanks for posting it! I do have one quick question. Here's the scenario:

    I have 2 pages – one for events, using the code above, and one for past events. When the event passes, I don't want it to disappear entirely, but I want it to display on the past events page instead.

    How would I alter the code above to do that?

  • http://twitter.com/ckarasiewicz Christian K

    How do you install this? I couldn't tell from the post.

    I am using Thesis and created a file called category_mynumber.php and put it in my templates folder. From there, I don't know where I need to post events and what the steps are when doing so. Can someone help?

  • Brennanleebaker

    Hi thank you for putting in the time for the code and giving to the WP community.

    But i have a problem with this method and wondering if you could help. The posts come out right and When the date is past set in the “eventsortdate” it doesn't display the post anymore. The problem is that the posts dont display in the order of the upcoming events. It rather looks like it still posts in the order they are published. I'm not sure if that was your intention but its happening to mine at the moment

  • Bazelski

    probably the best solution ever for showing future events . simple and clean

  • Sidjags

    awesome!! thanks so much for this.. after all the nonsense i’ve read and tried out about events on wordpress… this is by far the easiest!!! thanks a ton!

  • Rohan Kharbanda

    This is the best option for listing events wherein each event is a post. however you DO need a plugin to allow php in posts and widgets..thankfully there are a lot of em out there. One correction though, starting from WP3.1 the line..
    $eventdate = get_post_meta($post->ID, “eventdate”, true);
    will need to be changed to
    $eventdate = get_post_meta(get_the_ID(), “eventdate”, true);

    as $post->ID doesnt work for some reason…