WordPress Date Field
Notice: A new version of this plugin is now available on github.
Unfortunately, the new version is not compatible with this version.
I have used a couple of plugins to manage events/gigs before, but I found that in some ways, they tried to do too much. I wanted a simpler option that was more integrated with WordPress posts. I hope that my plugin provides a nice simple and versatile solution.
The Date Field plugin adds a panel to the add/edit post page that allows you to enter a date. It is essentially a custom field that converts the date to a unix timestamp and stores it in the post meta data. The plugin was derived from the Creating Custom Write Panels in WordPress tutorial on wefunction.com. A great tutorial for anyone looking to add custom write panels to their theme.
Using the Plugin.
The date field plugin can be used in much the same way that any custom field is used. Its real purpose is just to provide a simple way to add a date to a custom field, and to have that information stored as a timestamp. The meta key used is date_value. Remember, the value is a timestamp, so you should use the php date function to diaplay the information in the desired format.
string date ( string $format [, int $timestamp ] )
Here is an example for displaying the date within a WordPress loop.
echo date("l jS \of F Y", get_post_meta($post->ID, 'date_value', true));
//Prints something like: Tuesday 18th of March 2014
WordPress of course has the ability to order posts by a custom field, and this is ideal for displaying posts ordered by the date-field value. The code below will desplay all posts where date_value is in the future, and ordered by date (soonest first).
<?php query_posts( 'meta_key=event_date_value&orderby=meta_value&meta_compare=>=&meta_value=' . time() . '&order=ASC' ); ?>
<?php foreach ($pageposts as $post): ?>
<!-- Do stuff... -->
<?php endforeach; ?>
I hope to make some functions/shortcodes to be used with this field at some point, so it should be a little easier.
This is my first WordPress plugin, so I would appreciate any feedback, feel free to leave a comment or email me. Comments/Criticisms etc. are all welcome!
Version History
0.3
- Generally cleaned up the plugin. Fields are added to the array as before, but just adding a type = date option will bring up the field, rather than adding each part separately.
- Added a fields to add time.
- Fixed the issue id name issue Styedev pointed out.
0.2
- Made it possible to add other fields by adding items to the array. Keeping much of the behaviour from the wefunction.com tutorial intact like I really should have done in the first place.
0.1
- Initial Release

Hey,
First, the plugin is great! It’s helped me get over a hurdle I’ve come across quite a few times.
I’ve been using an query_posts() instead of your SQL string, I was going to blog about it mentioning your plugin and a different way to use it. Email me and let me know what you think.
Thanks again!
Nicely done!
I’m using WP as a CMS and I’d like to add a couple of additional features, mainly to help users in the admin interface:
A popup (js?) date picker, including hours and minutes and maybe a second one for an end time/date.
So far, I’ve had only limited success – my JS knowledge is weak at best. I was able to add the hours and minutes using the dropdowns. But if you have any thoughts on the rest, I’d sure appreciate it.
Benamin – I was going to look at the query_posts method, mostly because I want to be able to specify categories in the call. Can you share how you implemented?
Thanks very much!
Stace – I would quite like to use a popup date picker, although when I’ve seen them used in the past- I didn’t really fit with the style of the wordpress admin theme. No doubt this could be overcome, but like you, I dont have great knowlege of javascript.
Also- the next feature I want to implement is a “To Be Confirmed” option. Maybe after I’ve done this I will look at a date picker and adding time also.
Matthew,
Thanks for the reply!
I wonder if you might be able to help me with something I’ve been banging my head against for most of the day now, without success.
I work at an elementary school and want to use something like this for teachers to add homework. All I’m trying to add are two custom fields with drop-downs (one for subject and one for section).
I added the array and can make a selection but the selection doesn’t save. I think I’m doing something wrong in selecting the value for the new field; if I replace the variable with hard data in the save function, it does indeed save tot he correct field.
Any thought?
Stace
Sorry I didn’t get back to you sooner, had to work.
It was particularly awkward to add new fields to this plugin, and in hindsight I should have done it better.
However- I have fixed the problem, and if you add a new item to the array, an input field will appear. You can of course customise this field in the way I did for date/day/year/time.
It should save fine now.
No worries on delay in replying – I just appreciate the help!
I installed v0.2 and, as you said, just adding an item to the array adds a text box and saves the value. Thanks very much for finding the time to update this!
But I’m trying to use a pulldown (like the date fields) instead of a text box and I can’t seem to get it to work. I essentially copied the “elseif … month” section and swapped in “subject” for “month.” The pulldown appears with my array of choices but the selection doesn’t save. I think my problem is here, not in the save postdata function.
Obviously you’re much more skilled at this than I … would you mind taking a look at my code? I have a feeling that it’s something simple, yet beyond me.
http://pastie.org/513994
Thanks very much,
Stace
This plugin is great. Thank you for sharing it!
I wanted to let you know of an issue I’ve run across so that maybe you’ll be able to revise the code at some point in the future.
All of my dates after Daylight Savings Time ends this year in the U.S. (around end of October) were off by a day because the the time was then actually 11PM of the previously day rather than midnight in the correct ones. I hard-coded a time in the plugin code so that it wouldn’t be midnight by default and when the clocks get set back, it’s still the same day. Here’s the line:
$newdate = $month.”/”.$day.”/”.$year. ” 03:01:01″;
Thanks!
Thanks for pointing out this bug. Time was something I wanted to add anyway, but I guess this makes it a bit more urgent. I will sort something out next time I have a chance to update the plugin.
Hi Matthew,
Thanks for this very useful plugin! I had a tough time to look for a event plugin too, finally I found yours, it made my day!
I am new to php, but I try to change the month names into other language, for example, I want to display/echo the date like “13 agosto 2009″ in italian. I tried to read your php file, and translated the english month name into italian ones (line 101). Only when editing the post, they look fine. When displaying the post, the english version remain still.
I searched, the solution is to use setlocale() and strftime(), but where to put, I don’t know. May you consider it for next release please?
Thanks!
Changing the month names in the plugin will only affect the select options in the admin page.
“But WordPress supports a localization resistant date function, that should be used whenever you need month names at your prefered date outputs”(from code-styling.de):
So have a look at date_i18n().
I will look into this as I am working on an update.
Matthew,
Which file do I put the, “…php $today = time…” code into?
Hoping this plug-in helps me with the event site I’m working on … and, of course, I’ll give you link credit
—Kimberly
the code:
$today = time();is just to get the current date, which is then used in a custom WordPress select query.This goes in your WordPress theme file, to list all the posts with the date field set, that have a date stored which is in advance of the current date. Please see the page I linked to above for more details on how to do this.
In fact – as Benjamin Reed mentioned a while back- it would be easier to just use a query_posts() which can be done as follows.
query_posts('meta_key=event_date_value&orderby=meta_value&meta_compare=>=&meta_value='.time().'&order=ASC');This should be a little easier to understand – and again goes in your theme file and makes up part of the loop that will display all the events.
I am going to update my documentation with this method with the release of the next update, which should be soon…
Thanks for using my plugin – hope you find it useful.
Hey Matthew,
I noticed that when using your plugin in conjunction with another instance of the custom write panel it breaks your plugin.
I changed line 155 from:
add_meta_box( ‘new_meta_boxes’,…
to
add_meta_box( ‘df_new_meta_boxes’,…
and this fixed my problem.
Thanks – will fix that in the next update.
Hi, thanks for the plugin – just what I have been looking for!
I have a meeting page showing upcoming meetings using this, but would also like a list of previous meetings sorted by the meeting date.
Is there any way of using the time stamp to display the previous meetings as well?
Thanks,
Gitte
Well in the query_posts function, the bit
lists all future dates, simply changing to less than time() –
Or- you could just remove it to list all events – past or future.
Hi Matthew,
First, this is a great plugin and I thank you for building and supporting it.
Can you give us an example of editing the code to add a time field?
Updated the plugin finally – it now includes time. Please note that the method of adding fields has changed.
Awe-some. Thanks so much!
Hi, Matthew, I am using 0.2 version. I noticed that in the 0.3 version, you’ve changed the meta_key from date_value to event_date_value. My question is that how do I transfer the old values saved in date_value to event_date_value?
Anyway thanks for this plugin.
I apologise – I changed the wording when I really shouldn’t have.
I have added a new version that fixes this bug when upgrading. Do let me know If you run into any other problems.
I ran into a problem while using this plugin, mainly with my server default time-zone being different than my wordpress blog timezone. The problem causes the time to display wrong.
I added the following line in the plugin file to set it to my time zone:
date_default_timezone_set (“America/Detroit”);
The problem is that if your blog is hosted outside your timezone, you’ll have to manually add the correct time zone to the plugin for it to work. It would be great if the plugin could automatically pull the blog timezone from the wordpress settings/database. That way it would always be set to the blog’s default time zone, not the server’s default time zone.
So is that line all that is required to fix the problem?
I will sort this for the next release. I know something similar was mentioned before, but I have not been able to test it as my server is in the same timezone.
I’ll look into it next week.
Thanks for your feedback.
That is the only line that was needed for me, I suggest you pull the user set blog timezone form the wordpress mysql database so it will automatically set the timezone to the blog default timezone.
You could also make a custom write menu for the plugin and allow people to select a timezone from a list or something?
Since I’m in EST time the code I provided above will work for me.
hello, i am new about wp. I am triyng to install thios application but, after installed the plug in i can’t understand where to put the code written.
I am using a theme taken by whootheme, don’t know if this is a problem but i can’t understand where is the loop to insert the code.
Thenk you in advance if you can help me to fix it.
It shouldn’t be a problem to use this in a theme from woothemes.
Please see this page for how to use query_posts. Query_posts
It depends where you want the posts to go but I have used this plug-in by adding a new loop in the sidebar.
Hope that helps.
I just wanted to leave a note of thanks to you for your plugin. I have searched for days for something applicable to do just this. The ability to enter a date/time and have it stored that way instead of a string so that it can be sorted and filtered correctly. Excellent. This needs to be in the WordPress Plugin repository (or is it and i just couldn’t find it?)
I would also like to use your ‘Event’ box to enter a name and a link. Where would i add that? Is there some way to disable the time boxes and have just the date boxes?
Again, thanks for the work and keep it up!!!!
Pingback: How to create an archive based on custom fields, not publish date in WordPress | WP Garage
Pingback: How to create an archive based on custom fields : Wordpress Herald
Hey, this is good stuff. It did just what I was looking for…. I’ve got a site that I needed to list upcoming ‘events’ in chronological order from the soonest to the farthest away and this let me do just that.
Now, I just need to figure out how to do the same thing, but in the sidebar as an ‘upcoming events’ link area…
Awsome! Really nice work, i’m using it with some post type wordpress 3.0 capabilities and works like a charm.
I changed time() to mktime() – 24*3600 to make sure events that are happening today or “right now” are also listed.
Thats a great improvement – thanks. I will update the page as soon as I get a chance.
Thanks a million, Matthew!! Really appreciate the plug in
Thanks for the sweet plugin! Just wanted to mention that the query posted:
=&meta_value=' . time() . '&order=ASC' ); ?>
Doesn’t work by default because the meta_key stored is actually called “date_value”.
A the solution is simple:
=&meta_value=' . time() . '&order=ASC' ); ?>
Thanks for your solution. I realize that this plugin is not so user friendly. I have done a complete rewrite which I am in the final testing stages of testing – and I hope to release soon!
I’ve patched v0.3.1 to incorporate the jQuery DatePicker plugin, plus some other bug fixes & tweaks for my personal preference. Email me if you’re interested.