
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

- 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.

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).

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?

Custom Search
Alex Denning (21 comments)18 January 10
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 (1511 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!
Alex Denning (21 comments)18 January 10
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 (1511 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!
Nimit kashyap (1 comments)18 January 10
I am not that good with the code, is there any plugin that you think can do this automatically?
Michael Martin (1511 comments)19 January 10
There are a fair few events plugins in the repository: http://wordpress.org/extend/pl.....#038;sort=
Not sure of any that work quite like this though, sorry!
ecko (1 comments)29 January 10
I use one on one of my websites called “Simple upcoming events” By Hideaki Hayashi and it does pretty much the same only in the sidebar and the title links to the post
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 (1511 comments)19 January 10
Thanks very much for catching that Kristof! Going to fix it now! Sorry about that, pretty bad error to make!
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 (1511 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?
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.
Bill Robbins (2 comments)18 January 10
Thanks Michael. This is a great way to handle something that many companies and organizations really need on their site.
Michael Martin (1511 comments)19 January 10
No problem Bill, great to hear you like the approach!
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?
Kristof (7 comments)18 January 10
You can exclude the events category ID from displaying on the home page.
BigM75 (1 comments)18 January 10
thats great, i will make this on my sportspage
Michael Martin (1511 comments)19 January 10
Awesome, let me know how it turns out!
Jeff Mackey (4 comments)19 January 10
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
Jeff Mackey (4 comments)19 January 10
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?
Jeff Mackey (4 comments)19 January 10
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.
Jeff Mackey (4 comments)19 January 10
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 (1511 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.
redwall_hp (144 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 (1511 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 (144 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.
become a police officer (1 comments)19 January 10
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.
Eko Setiawan (3 comments)19 January 10
Great…this tutorial increase my knowledge. Because usually for events in wordpress .. I use the calendar.
Thanks…
Michael Martin (1511 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
Lee Ka Hoong (13 comments)19 January 10
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 (1511 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!
Jean-Baptiste Jung (4 comments)19 January 10
Very cool post, and very interesting use of WP. Well done, Michael!
Michael Martin (1511 comments)21 January 10
Thanks Jean-Baptiste, glad you liked it!
Justin Moore-Brown (3 comments)19 January 10
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!
Michael Martin (1511 comments)21 January 10
Good luck Justin, let me know how it goes!
comprar apartamento (1 comments)19 January 10
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
offset printing services (1 comments)20 January 10
Great tutorial thanks I was looking for something like this for wordpress.
Michael (22 comments)20 January 10
Wow! Exactly what I was looking for! Works great, thank you.
Michael Martin (1511 comments)21 January 10
Welcome, glad to hear it went well!
Adam Pieniazek (1 comments)21 January 10
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 (1511 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
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 (1511 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
)
Arfandia (2 comments)22 January 10
awesome tutorial.. i’ve to try this at my blog.. thanks
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 (1511 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 (1511 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.
Michael Martin (1511 comments)30 January 10
Hey,
It depends on where you want the events list to show up. e.g. If you wanted a list in the sidebar, you’d look in sidebar.php
If you wanted a page specifically for the Events category, you should check out this page to see how templates relate to that: http://codex.wordpress.org/Category_Templates
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.:(
redwall_hp (144 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 (1511 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
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….
Michael Martin (1511 comments)23 January 10
Good point Kimber, I hadn’t thought about any lost SEO benefits that some plugins might lead to!
Good luck implementing it on the Lebanon site, let me know how it works out!
Kimber (3 comments)25 January 10
Hey, I’ll probably throw it up next week…. I’ll post an update once it is working!
Michael Martin (1511 comments)26 January 10
Sounds great, hope it all works!
Martin (16 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
Michael Martin (1511 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!
Josh Stauffer (11 comments)15 February 10
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!
Adam Hermsdorfer (5 comments)25 January 10
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 (1511 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!
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 (1511 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;
}
Josh Stauffer (11 comments)15 February 10
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).
Hayloft Media (3 comments)28 January 10
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.
Man and a Van London (13 comments)11 February 10
Great post. I never done making a event list to my word press blog but I think I will try that one soon.
Bill (5 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.
Ralph Casafrancisco (10 comments)14 April 10
i have the same problem as your first issue, let me know if you figure it out …
as for issue number 2 … this is what i did. use this within the loop
$eventMeta = get_post_meta($post->ID, ‘Date’, true);
$eventDate = strtotime($eventMeta);
$displayDate = date(‘F j, Y – g:i A’, $eventDate);
all you have to do is adjust the way you want to display the date by adjusting the single quoted part of the date() function. Then echo $displayDate in your template loop
stephen (4 comments)29 July 10
not sure if i understand part #2 here….
the page does not load when i use that code. can you take a quick look?
thanks http://pastebin.com/uE0BxaJY
Ralph Casafrancisco (10 comments)7 April 10
i was having one small little problem, the function is completely working except for the sorting. any ideas?
WP_Query(‘showposts=5&cat=6,8&meta_key=Date&meta_compare=>=&meta_value=’ . $todaysDate . ‘&orderby=meta_value&order=ASC’);
George (4 comments)9 April 10
Hello, Michael. Great post and tutorial it’s exactly what I was looking for.
Michael can you create some PHP template how to use this function on our themes it would be much easier to understand and use.
Thanks again for great tutorial.
Looking forward
George
Michael Martin (1511 comments)14 April 10
Hi George,
All of the code needed is in the post. The actual snippet isn’t that long (10-15 lines really), a template for that would be overkill. Try working through the post until you understand each step, and then just experiment with it. You won’t break anything on your site, so it’s risk free
Monsoon (1 comments)9 April 10
Any chances you could post the actual php code you are using on your client’s sidebar and or event list itself?
If your meta is a date like mm/dd/yyyy 00:00:00 what code do you use to show something like
Mesquite, TX Activist Training
TUESDAY, JANUARY 19
I can t figure out the php code to show TUESDAY, JANUARY 19 instead of what is in the DATE custom field.
Ralph Casafrancisco (10 comments)12 April 10
read the comment i made for rebecca
Gadget Review (2 comments)10 April 10
will it support with new wp version?
Michael Martin (1511 comments)10 April 10
Yep, won’t have any issues
Rebecca (1 comments)12 April 10
Michael, how do you show the date as Tuesday, January 19 like in your client’s site?
Ralph Casafrancisco (10 comments)12 April 10
This is the code i added, and i modified it for the way you two wanted, this can probably be simplified, but it worked for me. Just add it in the loop before you add your code to display the date. Call the date with
@Monsoon & @Rebecca
$eventMeta = get_post_meta($post->ID, ‘Date’, true);
$eventDate = strtotime($eventMeta);
$displayDate = date(”, $eventDate);
squeaky (4 comments)13 April 10
hi there, this looks like it is exactly what i need. i am however new to wordpress.
i want to put the code in my index.php, but i can’t figure out where and how to exactly add it to that file.
any help would be greatly appreciated!
thx
squeaky (4 comments)14 April 10
okay i’ve figured out the sorting of events. Now i’m trying to add the code as Ralph suggests to show the date. but here i’m also not sure about hwo and where to put it. i have the in the loop but don’t know where to put Ralphs code to get correct date display.
again any help would be much appreciated!
Ralph Casafrancisco (10 comments)13 April 10
The sorting is not working for me. The difference that I did was that I have separate categories for different events and i think that is what is making it bug out. any suggestions out there?
Michael Martin (1511 comments)14 April 10
Hi Ralph,
Thanks for answering a few other people’s questions here, that was really good of you.
I’ve had a look at the query you posted above, and as far as I can see, it looks like it should be working. It looks identical to what I’m using (and is working) on the client’s site.
If I were you, I’d save that query and then start from the start again. Try using exactly the query I had above. That should work. When it does, change one thing (e.g. to whatever categories you’re using), i.e. just keeping checking that it’s working as you make each change. Eventually it will stop working and you’ll know what’s causing the trouble at least.
Sorry that’s not a great help, but the first step in bug fixing is always to find where exactly the bug is
Ralph Casafrancisco (10 comments)15 April 10
Michael,
I did try again from the beginning and I was still not able to make it work. I think there is something going on with the meta_compare. I tried to create an event that had a time that has already past yet the event is still shown, not to mention my issue with the sorting that I mentioned before. I even tried creating a new category to link those events into a single category, but that did not work either.
The thing is, I am using this loop after the main content so I did not use the query_posts function that you have. So rather I used a new wp_query and stored that in a variable, then i used that variable to link the new loop. Here is my code for that section of my footer.
Get Involved!
=&meta_value=’ . $todaysDate . ‘&orderby=meta_value&order=ASC’);
?>
have_posts()) : while ($event_query->have_posts()) : $event_query->the_post();
$eventMeta = get_post_meta($post->ID, ‘Date’, true);
$eventDate = strtotime($eventMeta);
$displayDate = date(‘F j, Y – g:i A’, $eventDate);
?>
<a href="”>
No Upcoming Events
Any suggestions would be appreciated Michael, I’m pretty sure you are better than me with this since I just started with multiple loops.
Summary:
Sorting does not work.
Determining if the event has past does not work.
Ralph Casafrancisco (10 comments)15 April 10
sorry about that, lets try again
Get Involved! =&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?> have_posts()) : while ($event_query->have_posts()) : $event_query->the_post(); $eventMeta = get_post_meta($post->ID, 'Date', true); $eventDate = strtotime($eventMeta); $displayDate = date('F j, Y - g:i A', $eventDate); ?> <a href=""> No Upcoming EventsRalph Casafrancisco (10 comments)15 April 10
http://rcdteam.com/eventlist.jpg
Ralph Casafrancisco (10 comments)15 April 10
sorry for flooding the thread … i figured out my problem … thanks for trying to help everyone … i found out that my issue with the sorting was with the way that the date in the custom field was formatted … some events had the leading zero for the month which confused the sorting … i adjusted the format of the date and it fixed both problems of sorting and removing past events.
Thanks for the script Michael!!!
if any other newbies need help and need me to explain what i did feel free to email me
squeaky (4 comments)15 April 10
Hi Ralph, i’m still strugling with the bit of code you posted to get the date to display correctly. (I am very newbie btw) anyway for some reason the way you implemented your code doesn’t work for me. is there anyway i can contact you about this, so i don’t clog up this thread..?
thx
Ralph Casafrancisco (10 comments)15 April 10
@squeaky my email is ralph.a.casafrancisco@gmail.com
squeaky (4 comments)14 April 10
I don’t know if i’m doing something wrong but in my code for the sorting to work properly and put post in the archive after midnight (new date)
in the query_posts i had to change :
meta_compare=>=&meta_value
into
meta_compare=>&meta_value
any one else notice this or is it me who’s doing something wrong?
Szczecin Imprezy (3 comments)15 April 10
This tut is great it is exactly that i’ve been looking for to manage the cultural ivents in My Town… I put the code to fit with my tab informations system and it’s perfect. One think I have to change was the time/date format to mentioned above: yyyy/mm/dd. Now it is awesome. regards to all that make this thing working!!! Kierunek Szczecin!
Scotty (1 comments)17 April 10
Awesome tutorial Michael. I’ve tried out serveral plugins and none of them did exactly what I was looking for…just to be able to list out the events with some sort of sorting.
I do have one question. Is there any way to show a list of states at the top and have the user choose a state and only show events from that state? Maybe show the states in a pull down menu?
Thanks again mate. Great post.
George (4 comments)17 April 10
Hi, Michael.
Yes, I understand how this code works in post, but now I’m trying to show the events on sidebar like you did for http://americanmajority.org/, and it’s still hard for me (as I’m beginner) but still working and experiment with it.
One more question about sidebar did you make some upcoming events widget for American Majority website?
Thanks for code and good examples
Geroge
Shait (1 comments)17 April 10
How it is possible to “hide” newest events in case if order is Descending. For example we have one event per month and we woud like to have Upcoming and past events as one “list” so we need to put upcoming Events in DESC order. And if we limit number of posts on 5 in this case wordpress show events on the end of the year and not events which will be in next months…
=&meta_value=’ . $todaysDate . ‘&orderby=meta_value&order=DESC’); ?>
What function we need to use instead of showposts=5?
Szczecin Imprezy (3 comments)21 April 10
It’s almost a week when I start using it – still no problem with it on my pages. Came back just to say one more THANKS! Regards from Szczecin
daniel (13 comments)24 April 10
Hello guys, i need your help.
I have one blog for events in my city and i use one calendar theme, i send many post with events for one date in the future. And this is my problem, if i enter in single post, for example with date in future 22-Jun-2011 i see one error 404 don´t exist.
I use
in single.php but not workig, any idea to see all post included future post for all people in single post?
Ramsey Stoneburner (1 comments)28 April 10
Awesome tut!
Like Jeff Mackey above (Jan/19), I wanted to use the wordpress built in date posts to schedule my upcoming events but then display them to the users. It seems to work great that way. I was having problems using the custom field date thing due to having my date printed as Month, Day, Year (which is how the client wanted). Plus, using the built in date stamp, I can pull posts from various time frames and display them in other areas of the site. Thanks!
King Rosales (1 comments)1 May 10
Awesome post! Ive been looking for a event list solution for a little while now, merci beaucoup Michael
Michael Martin (1511 comments)2 May 10
Haha, I’m afraid my French is failing me here. [french]You’re welcome![/french]
Rocco (1 comments)2 May 10
First off, thanks for posting this information. I’m a complete WordPress newbie – just installed it about 45 min ago after looking at some Calendar/Event plug-ins and then finding this page… since that time, I’ve been fumbling around trying to get this to work. I added a few “event” posts, and then drove myself crazy trying to figure out where to add your code. I tried adding it to a “Page” then a “Post” and ran myself around in circles for about 30 minutes, not realizing that I had to add your code to one of the php files associated with the Theme I’m using (no idea which one yet).
I guess that you didn’t plan on writing this for a WordPress novice, but including a line or two about where (in general) to put the code would have saved me some aggravation.
Thanks!
laurence (1 comments)3 May 10
I have implemented the code as followed on your tutorial… but the problem is that it goes to other post… and when I click on the “Events” category my post is not showing up.
George (4 comments)6 May 10
Michal I’ve one question. Did you create special widget for “American Majority” to display upcoming events on sidebar?
P.S. I think we could use this idea more useful with WordPress 3.0 and its new Custom Post Type function.
christopher (1 comments)11 May 10
Great stuff here.
Everything is working for me perfectly.
The only issues I’ve had so far are getting dates to display as “Monday, May 10″ instead of the default 05/10/10. I tried the example given above but I didn’t have much luck with it.
And I’m also trying to figure out how to include recurring events like something that happens every monday.
But this code is very simple and works fantastically.
Brandon (3 comments)17 May 10
Hi Michael,
Great post. I was wondering if the following is possible.
I have a publication that has multiple publishing dates and will be assigning a few custom fields for those dates.
Example:
key1 = “eventdate1″ value=”01.09.2010″
key1 = “eventdate2″ value=”03.09.2010″
key1 = “eventdate3″ value=”05.09.2010″
key1 = “eventdate4″ value=”09.09.2010″
Is there a way to query this “One” post and make its title, excerpt and more link show up in a list separately ordered by date from all four custom field values?
Example:
01.09.2010 Publication title
Excerpt text goes here … more link.
03.09.2010 Publication title
Excerpt text goes here … more link.
05.09.2010 Publication title
Excerpt text goes here … more link.
09.09.2010 Publication title
Excerpt text goes here … more link.
Your time is must appreciated. And I’ll give you credit in the template if you can get this to work.
Thanks!
Agnes (2 comments)18 May 10
Thanks a million for this post, I’ve tried a bunch of plugis but none that provide the simple functionality of listing certain events only on certain pages. However, instead of hard-coding the name of the tag (tag=texas) I would like a more dynamic solution for listing events in the sidebar or footer. I’ve tried something like this but to no avail:
$todaysDate = date(‘m/d/Y H:i:s’);
$current_page = wp_title(”,false,”);
=&meta_value=’ . $todaysDate . ‘&orderby=meta_value&order=ASC&tag=$current_page’); ?>
Any pointers would be greatly appreciated. (Worst case scenario I’ll make like 7 different sidebars but that seems so unnecessary and crude…)
Christine (4 comments)25 May 10
Thank you for this script however I need a little help getting the correct date to appear on my events. I have a Date Custom Field in the format of m/d/Y H:i:s and I added the following the code to my template however the date on all my events displays as Thursday, January 1 1970 00:00 am. You can view here: http://davidsonfarmersmarket.org/upcoming-events/
Thanks for any help.
=&meta_value=’ . $todaysDate . ‘&orderby=meta_value&order=ASC’); ?>
ID, ‘Date’, true);
$eventDate = strtotime($eventMeta);
$displayDate = date(‘l, F d – H:i a’, $eventDate);
?>
<a href="”>
Christine (4 comments)25 May 10
Sorry… the code didn’t paste correctly. Here’s the code to my last issue with the date not displaying correctly:
Get today’s date in the right format
$todaysDate = date(‘m/d/Y H:i:s’);
query_posts(‘showposts=20&category_name=events&meta_key=Date&meta_compare=>=&meta_value=’ . $todaysDate . ‘&orderby=meta_value&order=ASC’);
have_posts()) : while (have_posts()) : the_post();
$eventMeta = get_post_meta($post->ID, ‘Date’, true);
$eventDate = strtotime($eventMeta);
$displayDate = date(‘l, F d, Y – H:i a’, $eventDate);
Chris Ruggia (2 comments)31 May 10
This is definitely a useful contribution to the WordPress world!
I was using the WordPress Date Field plugin to do something similar, but between your tutorial and Ralph’s strtotime() suggestion, I was able to do away with that extra plugin!
I’ve now made a sidebar widget using your idea that also allows for multi-day events (it uses startDate and endDate custom fields, formatted like so: 2010/3/15 – year first, no leading zeros in month or day, no time of day) and lists the date (or date range) and title with a link to the post under the heading “Upcoming Events”.
This is my first-ever widget, so it would be silly to publish it, but if anyone wants the code, here it is: <a href="http://www.vastgraphics.com/sidebar-event-list.html"http://www.vastgraphics.com/sidebar-event-list.html
Copy and paste the code into a php file and put it in the plugins folder, then activate and the widget should show up in the list.
If it doesn’t work, it’s probably my fault. I’m just a beginner myself.
Chris Ruggia (2 comments)31 May 10
As I was implementing my widget, I found that the leading zeros in the date were necessary for the sort to work. Alphabetically, 5 apparently comes after 24, but 05 comes before it. So the starDate and endDate formats are now: 2010/01/09 for January 9, 2010.
I updated the code, if anyone wants it: http://www.vastgraphics.com/si.....-list.html
And now I will be quiet.
mike (15 comments)2 June 10
Thank you very much for this!!!
chryss (1 comments)2 June 10
Hy guys, thnks for this tutorial, but i have one question
How i can to display the first event from today, not from tommorow.
Thanks for your reply.
GOOD JOB.
Hirsch (1 comments)2 June 10
Thanks for the great post, this has really come in handy!
Quick question for you though. I want to create a past events page, and have it display all of the posts in the events category except for those that are still upcoming. What would be the code to do that?
David (23 comments)10 June 10
Hi, this is just what I’ve been looking for so many thanks for that. I’ve got it kind of working but merging the code with my themes code is causing me some issues. For some reason I’ve lost the ‘next’ and ‘previous’ buttons at the bottom of the page when I use this code.
My original code is: (which works with the pagination & date order but doesnt remove past events)
query_posts(“cat=$portfoliopages.&paged=$paged&orderby=DATE&order=ASC”);
and when I’ve adapted it:
query_posts(“cat=$portfoliopages.&paged=$paged&meta_key=Date&meta_compare=>=&meta_value=” . $todaysDate);
Any ideas? I’m a php newbie so struggling a tad…
Steve (19 comments)5 July 10
We are successfully using the future events posts script which is great. Is there a way to use the same sort of query, but show previous events from the past but still order them by the custom field?
Steve (19 comments)5 July 10
Here is the query we are trying but its just pulling out all the event in the category ordered by when posted rather than by the custom field
Steve (19 comments)5 July 10
Matt (16 comments)9 July 10
Nice code guys.. Good tutorial too.. How ever any idea how to reformat the date to stack like… http://www.pimpmywordpress.com.....te-bgr.jpg I do not need the background image but would like to stack the info like you see below..
TUE
16
JUNE
Bill (5 comments)21 July 10
I love this solution. Thank you so much. I’ve tried just about every plugin I could find and not getting what I want/need.
I’m wondering if there’s a way to accommodate events — like conferences, for example — that run a number of days. The listing would then appear as:
My Great Conference
December 10, 2010 through December 13, 2010
or something like that. I’d still like to be able to list single day events, too. Gravy would be using a manually-created excerpt! (I don’t want much, do I
)
Fairly new to WP and a PHP novice — but I sure can follow directions!
Thanks so much
Bill
bazel (1 comments)28 July 10
nice tutorial.
Can somebody help for limiting the list to show only events from the next 7 days, 30 days?
Thanks
Martin Dvorak (1 comments)29 July 10
Hello,
does anybody know how to make/where to get a script or a plugin to WP that would not only manage/display events, but also have a function for WP users to sing up for each event?
I need this for a poker-room site, where the staff will manage the games and users will sing up.
stephen (4 comments)29 July 10
My meta “date” field looks like this:
m/d/Y H:i:s
so I can sort events in order, even if they’re on the same day. But let’s say I want to echo the date (and not the rest of the custom field… H:i:s) how can I do this??
Chief (2 comments)30 July 10
Michael, Excellent post. Thanks for spelling it our so plainly. I’m going ot give it a try.
Paula (17 comments)9 August 10
I’m just starting into web development!!!!
Mateus Neves (2 comments)16 August 10
Man, this article is excellent. And very helpful.
Tanks