Showing a WordPress post’s ‘last modified’ date

I’ll occasionally return to a post and revise it for improved methodology, test results or whatever. But the ‘posted on’ date always remains the same, even after the post has been updated. I feel that displaying only the ‘posted on’ date could be somewhat confusing, particularly when I state that I’ve updated the post in a comment dated months later. So, in the interest of full disclosure, I have added a few lines of code to the WordPress ‘single.php’ template file to supplement each post’s meta data with the date it was last modified. If the post has never been modified or if it was last modified within 24 hours of the ‘posted on’ date, only the ‘posted on’ date is shown.

Of course, this could be used anywhere inside the WordPress loop, not just in the meta data section. The code I use to show a WordPress post’s last modified date and time is as follows.

The default Kubrick template’s meta data section:

<p class="postmetadata alt">
<small>
This entry was posted 
...
on <?php the_time('l, F jS, Y') ?> 
at <?php the_time() ?>
and is filed under <?php the_category(', ') ?>.
You can follow any responses to 
this entry through the 
<?php comments_rss_link('RSS 2.0'); ?> feed. 

The new code, modified to selectively display the last modified date:

<p class="postmetadata alt">
<small>
This entry was posted
...
on <?php the_time('F jS, Y') ?> 
at <?php the_time() ?>
						
<?php $u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
echo "and last modified on "; 
the_modified_time('F jS, Y'); 
echo " at "; 
the_modified_time(); 
echo ", "; } ?>
	
and is filed under <?php the_category(', ') ?>.
You can follow any responses to 
this entry through the 
<?php comments_rss_link('RSS 2.0'); ?> feed. 

You can see how this works in the meta data section of this post.

Further customization

I’m using a grace period of 24 hours from the time the post was published, but you could change this by replacing 86400 with however much time you want, specified in seconds.

22 thoughts on “Showing a WordPress post’s ‘last modified’ date

  1. Pingback: Patrickone Dotnet » Blog Archive » Adding “date last modified” to a WordPress post

  2. Pingback: Showing “Last Modified” Date in WordPress « planetOzh

  3. Pingback: Dale al teclado » Cómo mostrar la última fecha de actualización de un post en Wordpress

  4. Pingback: The Sharing and Caring of the WordPress Community Shines « Lorelle on WordPress

  5. Bruce

    Ardamis, thanks for this code snippet: I really can’t understand why there isn’t such a function built in to WP. You say “If the post has never been modified, only the posted-on date is shown”. It looks like I modify a lot of my posts on the day of publishing e.g. adding “alt” tags, correcting spelling. I’m a perpetual PHP beginner but I’d like to display the following code if modified date > post date by 24 hrs – to allow for such trivial edits (and it makes no sense displaying a post date and modification date that are the same anyway):

    < ?php /* Last modified */ $u_time = get_the_time('U'); $u_modified_time = get_the_modified_time('U'); if ($u_modified_time != $u_time) { echo "Last modified "; the_modified_time('F jS, Y'); echo ""; } ?>

    Is this possible, or am I looking at a plugin to do this?

  6. ardamis Post author

    Wow. Yeah, I should have thought of that when I wrote the code. I’m just the same, always catching a typo or spelling error a moment after publishing a post, so many of my posts have a modified date that is the same as the published date.

    I’ve updated the code to allow for a 24-hour grace period after publishing a post, during which any editing won’t show up as a “modification”.

    Thanks for the great comment. This blog is better for it.

  7. Pingback: How To: Showing A Last Modified Date on Your WordPress Posts

  8. Pingback: Weekend Roundup #19 » JaypeeOnline // Blogging News & Reviews

  9. Pingback: Weekend Roundup #19 - PinoyPortal

  10. Pingback: How To: Showing A Last Modified Date on Your WordPress Posts | [Blog Tutorials]

  11. PokerAnon

    I do a lot of updating at later times, usually just formatting the modifications in italics, but this is great. Although, now it shows all the times I’ve made grammatical corrections as well. 🙂
    Do you think this helps the modifications to be picked up by search engines? Sometimes I rewrite 1/3 of the post and would like the revision to be noticed.

  12. Pingback: systemBash » Last Modified Date or Time on Wordpress Template Page

  13. Pingback: Mike Olaski Blog Archive » Display last modified date for wordpress post

  14. morten

    Nice – this really helped me. Do you happen to know why this doesn’t work for pages? It seems that get_the_modified_time() only returns modified time for post, and not pages.
    I’d like to show a little “Updated” icon next to the pages that has been changed after a 10 day period, but I can’t figure out how to get the modified time for pages :-/

  15. ardamis Post author

    Hey there, Morten.

    Strange that it’s not working for you on Pages. I use the_modified_time('l, j F Y'); on each of my Pages to display the date it was last changed, and it works.

    I haven’t tested, but the_modified_time() simply filters get_the_modified_time(), so I’d assume that get_the_modified_time() must work.

    Try putting in the_modified_time('l, j F Y'); where you want your icon, just to be sure that the last changed date is appearing.

  16. Pingback: Display the Last Modified Date for your posts on Wordpress

  17. Pingback: Menampilkan tanggal update pada postingan terakhir | triindi

Leave a Reply

Your email address will not be published. Required fields are marked *