On a recent client project, I was needing a way to order events based, not on the actual published date of the post, but rather by the date of the event in WordPress.
The solution I (finally) found involves sorting the posts in the Events category based on the value of a certain custom field.
Before finding the solution though, I tried several different ways to get events to show up, ordered from newest to oldest., but nothing I came across worked completely.
What Didn’t Work
In order to save some people who are looking into doing something similar, I’ll tell you what didn’t work for me. Some of the things mentioned here might work for your project, but it didn’t work for me.
WordPress Plugins
I started out trying not to reinvent the wheel and just go with a WordPress plugin. However, I couldn’t find anything that fully met my needs.
Calendar Plugins
I tried using several calendar and event plugins to get the job done. These might work for you, but I needed to have each event have its own post page. This was because each event had a registration form for the individual events.
Most of the WordPress plugins I found, either didn’t allow linking to actual posts—they only showed a summary. Many of them used a non-WordPress system to store the details about the event. Some just didn’t work at all and gave errors.
Event Plugins
I had similar issues with WordPress event plugins as I did with the calendar plugins. They had their own system of storing events (not as posts) and many of them where too rigid as far as options.
Future Post Plugins
I also tried using a future post plugin to make it appear that posts that were scheduled for a future date (say the date of the event) were actually published.
All the plugins I found apply this to all posts in all categories with no way to control it. So if you ever scheduled any post in any category to publish later, it would be published immediately—not just your future events.
Other WordPress code
In the meantime, I tried different code offered on sites to help with this and some would work partially, while others would not.
One “solution” was to schedule the post to be published on the date of the event and then use some PHP/WordPress code to show all the posts tagged as future posts in a certain category.
I was excited about this and it looked like it was exactly what I wanted.
The flaw with this plan is that while it will show the future scheduled posts and they will be all in the correct chronological order on a page pulling in posts, you can’t actually view individual post unless you’re logged in as administrator. People not logged in will get a 404 File Not Found message when trying to view the full post. This is just the way WordPress is designed and I couldn’t find any workaround for this.
The Solution, Already
I know, a lot of build up for the solution. Well, here it is.
Part 1: Flutter Plugin and Custom Fields
I was already using a great plugin called Flutter, which lets you create custom write panels and work with custom fields and all in a much more user-friendly way.
I created an Events write panel in Flutter and made an event_date custom field. I used this to display the date of each event instead of the published date of the post on my main Events page.
Flutter’s documentation is pretty sparse, and I could not get the sort by custom field to work using the code examples on their site, so…
Part 2: Order Using a Custom Function
Since the custom fields made by Flutter are just regular WordPress custom fields, we don’t have to use Flutter’s code to order them.
I found a great post and bit of code on Think Press‘ site:
Order Posts by Custom Key Revisited
Take a look at the above bit of PHP code and make these adjustments:
- Wrap the code provided in PHP tags (see comments if you’re unsure how to do this)
- Copy and paste that code into your functions.php file in your WordPress theme.
- Adjust the if statement to match your page or category to your needs
- Change to the custom field you want to sort by on this line (change ‘Price’ to your custom field):
$where.= "AND $wpdb->postmeta.meta_key = 'Price' ";
This worked like a charm. All the events are ordered from newest to oldest—based on the event date, not the publish date. This also works on any custom field; not just those used in Flutter.
Recap
Here’s a recap of my basic process:
- Created an Events category in WordPress
- Used Flutter to set up custom fields for the event date, time, etc
- Set a custom single post template for posts in the Events category (to show event registration code)
- Set up my Events page to pull in those posts
- Added the code found at Think Press‘ site to my theme’s functions.php file
- Replaced the post publish date with the custom field for the event’s date in my templates
Some of the above steps may not apply to your project, but hopefully this will save you some time when needing to sort by custom field in WordPress and when working with creating events.

You should

31 Comments to 'Sort Events by Date in WordPress Using Custom Fields'
March 6th, 2009
Hum. Although this is exactly what I was searching for a long time, I cannot make it work.
I have a page “Events” and a category with the same name.
I want to display all events (articles assigned to this category) sorted by my custom field “event_date” which is yyyy-mm-dd.
Although I assigned a new template (events.php) to my Events-page, I do not manage to “pull in those posts”!
Could you post an example of your working loop?
Thanks,
Michael
March 6th, 2009
Hmm… Well, since everyone’s templates (and loops) are going to look different, I don’t know how much help my personal loop will be to you.
How are you pulling in your custom fields?
`< ?php $page_summary = get_post_meta($post->ID, 'page_summary', true); ?>echo $page_summary ?>`
This will pull in a regular WordPress custom field (if the custom field key is ‘page_summary’). Be sure to take out the backticks at the start and end of the above code.
Also, have you 1st tried pulling in your posts from your Events category without using the code mentioned in the article? Does that seem to work? If not, it probably is an issue with your loop.
If you’re still having problems getting your events to display, drop me an email and I’ll send you a copy of my loop code.
March 8th, 2009
Hi, I think your solution is exactly what I was looking for but being a php nono I am having trouble making it work. I would very much appreciate a little help !
I have 2 custom fields 1 called “special” with value “1″ to retrieve posts ,this works fine using code found on WP Codex. I would like to list them on page sorted using a 2nd custom field called “tri” with value from 1 to XX. I have found the theme function.php file and pasted code after changing page name but how do I wrap the code with php tags
all I get is php errors
add_action(‘wp’, ‘check_page’);
all I get is php errors again
What am I doing wrong ???
Thanks for help
March 8th, 2009
If I understand what you’re asking, you need to wrap the code that you paste into your theme’s function.php in PHP tags like so:
< ?phpand
?>Notice that all the other code in the function.php file has opening and closing PHP tags like the above. Hope this solves your problems.
March 10th, 2009
Hi this is slightly off subject. I am trying to create a listings site and I stumbled across Flutter. AMAZING. The only thing is I am having a few minor problems in requiring the information.
Problem one: Is there anyway of showing ONLY the fields with a value? When I use the get function \\
Problem two: how do i show these so they sit on one line rather than each echo going onto a separate line. For example I have two fields one for start date and one for end date
What I would like it to look like is
March 19, 2009 – March 25, 2009
Currently the two dates are on separate lines. And there is no dash (this would need to be added separately.
Sometimes there will only be a start date so the – and end date are not neeeded… Any ideas??
I dont mind working on it but I dont know where to begin… Im ok with CSS but php etc just confuses me!
March 10th, 2009
Sorry i pressed the submit before I had finished…
I was wondering if you had done anything similar on your site or if you could possibly point me in the right direction?
Many thanks for any advice you can offer
Claire
March 10th, 2009
To get your echo’d fields from Flutter, you could use something like this:
`
< ?php echo get('event_date'); ?> < ?php echo get('event_time'); ?>
`
The echo’s don’t imply any HTML structure, so you don’t have to worry about the 2nd echo’d field causing a line break or anything.
As for only showing fields if they contain a value, you have a couple options:
1) The easiest way, but might not work for you, is to combine the start and end dates into a single custom field — just let them add the dash between dates if needed. The field can still be sorted via the way mentioned in the post.
2) Use a condition statement to check if there’s a value in the end date field.
Take a look at a post at Designphilosophy.com.
This is could be a whole ‘nother discussion, so I’ll just give you a few links to look at that should help you. Remember: custom fields created in Flutter are still WordPress custom fields, so you don’t HAVE to use Flutter’s code to pull them in – you can use regular WP php code.
WP Custom Fields
WP Support: Display custom field only if value present?
Custom Fields Tips and Tricks (list-your-blog.com)
March 11th, 2009
Hi Shawn thanks so much for your help! I have managed to do it using “Conditional display of custom-field data” on this link you provided http://www.list-your-blog.com/.....nd-tricks/
I also managed to solve the dash problem by using the print function within the end date ‘if’. Has worked a treat!! Now just many many more things to solve!!!
Thanks for your help!
Claire
June 16th, 2009
Hi. Thanks for the post. I also need to set up my Events category to list posts by a date created through a custom field, rather than wordpress’ timestamp.
i’m using the date picker in More Fields rather than Flutter, but the custom field outputted still looks the same as flutter…05/12/2009, for example.
however, when i plug my info into the function given above, and replace Price with speaking-date (my custom field key) it doesn’t list them according the the custom date.
am i missing something?
June 24th, 2009
This is exactly what I have been trying to do for some time now, many thanks!
If interested, ThinkPress now has an even simpler script: http://www.think-press.com/246.....stom-keys/
July 25th, 2009
I am deliriously happy I found this because I am creating a band website, and they need to be able to enter their show information and have it show up according to the show date, not the date they’ve posted it. Thank you so so much for clarifying the code from the ThinkPress site!
July 25th, 2009
@Heather – I was in a similar situation just before writing this post.
I’m glad this helped you out.
@Tyson – Glad to help. Thanks for the heads up on the new script. I’ll have to try it out.
July 27th, 2009
@Heather –
hey heather,
have you managed to get this code working?
George
London, UK
July 29th, 2009
Ya sorting by date does not seem to work. Dates like july 23 june 5 etc
August 11th, 2009
Hi Shawn,
Is it possible for you to shed some light on my issue. There’s hardly any documentation on the Flutter plugin.
I am simply trying to set up a site with upcoming events, and have wordpress list the most recent events from whatever the current date is and to archive the events that occurred before the current date.
I’ve created an Events panel, in Flutter, and a field called ‘events_date’. That’s the only info I need to have, the rest of details go in the post itself.
This is my code before using Flutter:
—————————————-
<a href="” rel=”bookmark” title=”">
<a href="”>
—————————————-
But now with Flutter, I’d like to pull 6 upcoming events from the current date.
Is there anyway you can shed some light on how I can code this ?
Thank you soo much in advance.
Amanda
August 11th, 2009
sorry this is the code!
php query_posts(‘cat=ID=3&showposts=6′); ?>
<a href="” rel=”bookmark” title=”">
<a href="”>
<?php endwhile;
August 11th, 2009
Here’s some more extensive documentation for Flutter:
http://www.scribd.com/doc/8790.....umentation
See pages 4, 5 for getting custom fields values and ordering based on custom fields.
ThinkPress has some updated code to order upcoming events (dead-easy) without Flutter:
http://www.think-press.com/246.....stom-keys/
Hope this helps.
August 12th, 2009
a little bit yes. almost there.
I’m stuck here –
// query_posts(‘meta_key=event_date&orderby=meta_value&order=DESC’); //
it’s displaying ALL events, I’d just like it to display ones that are greater than or equal to the current date.
Can this be done within that code or do I have to write a separate function?
Thanks again,
Amanda
August 12th, 2009
@Amanda Shaw – You’d need to grab the current date with php the_date(‘m,j,Y’). Store it in an array so you can use it to compare.
Then compare your current date (the_date) and set your function so that only events with dates (meta_key=event_date) equal to or greater than the current date value are displayed.
I’m not sure about archiving the older dates, but maybe this will point you in the right direction at least.
September 12th, 2009
Hi, interesting post on flutters possibilities! I do however think that some of the commenters would be better off using the Gigs Calendar plugin – Check it out, it’s easy to learn and to modify..
http://wordpress.org/extend/plugins/gigs-calendar/
September 12th, 2009
Hey, thanks for the suggestion. That might work, depending on what your requirements are for the events.
October 25th, 2009
I also have a similat question.
how do i link event date in the sidebar calendar.
I am using flutter to get the event date and want to link it in sidebar calendar to the event happening date.
Currently it only links it to the publish date.
November 16th, 2009
@Amanda Shaw
This is what worked for me. Only showing events in the future.
Remember to format today’s date similar to event_date.
=&meta_value=’.$today.’&orderby=meta_value&order=ASC’);
?>
November 16th, 2009
Sorry, most of my code got stuck in the filter;
// query_posts(‘cat=3&meta_key=event_date&meta_compare=>=&meta_value=’.$today.’&orderby=meta_value&order=ASC’); //
January 4th, 2010
Is Flutter necessary in this equation, other than it makes posting custom fields to posts easier?
I use “custom fields template” plugin to add fields automatically to my post category “Gigs”.
In any case, my gigs are not sorting. I’m formatting them as follows: mm.dd.yy
Does format matter? This is an important point that was ommitted from the article.
My code is as follows…
I changed the IF statement to:
if (is_category(8)) {
and the final statement to
$where .= “AND $wpdb->postmeta.meta_key = ‘gigDate’ “;
January 4th, 2010
I am getting this error:
Object of class wpdb could not be converted to string
January 9th, 2010
Hi there! Thanks for a useful post, it really helped me but I haven’t crossed the finish line yet, I’ve ran into some problems.
I want to show a list of future events (like other people in this thread) and my query looks like this:
// query_posts($query_string.”cat=3&customorderby=x_event_date&order=ASC”); //
I basically want to do what Shawn described and I have tried Olles solution, but I could not get it to work.
Im not using the script/code in the functions.php, because it gives me “Object of class wpdb could not be converted to string” as Jesse also experienced.
I hope someone can help, thanks! I would appreciate :)
February 2nd, 2010
@Marc Falk
@Amanda Shaw
@Shawn
Same issue with displaing JUST future posts here. Can you give us some further leads? Thank you very much!
February 25th, 2010
Awesome! I’ve been trying to get an Event calendar to work and this fixed all my problems so Thanks you Thanks you Thanks you!
February 25th, 2010
Events in WordPress are one of those things that you would think would be easy to do, and yet…
Glad this helped you :)
February 28th, 2010
This seems like just what I need, but it’s still not working for me. I fixed the errors in the code you linked to (see comments at bottom of the linked page–copy/paste reformatted some of the code, which is why others might be getting errors) but it’s still giving me errors. Does this have to go in a particular place in the functions.php? Is it possible that it doesn’t work for some functions pages? I just get errors when I insert it. I have tried all of the meta_key/meta_value options in my events template carefully, too, but no luck (other queries work).
Subscribe to the Comments RSS feed—Follow the discussion.
Leave a comment
Note: All links in the comments are set to 'nofollow'. Search engines will not follow or index those links.
Follow this post's comments by subscribing to the Comments RSS feed.