Showing the Last Updated Date in wp Posts or Pages

One of the most important things currently is to be up to date, offering contrasted information and, above all, that has not become obsolete. Most WP themes show the date on which an article was created but … How can a visitor know if that content is up to date?

Computer Tapes

The answer is to show, in addition to the date of creation, the date of the last modification of any wp content. In this post I’m going to show how we can do it in a very simple way.

Last Updated Date

When a user creates a new content for WP, then WP creates a new entry in the wp_posts table of its database -the table where it saves the information of all posts, pages, custom post types, menus, etc.-. This table stores a variety of informacion for each one of its entries: title, text content, author, URL, of course, the date of creation. But it also stores the date on which the author modifies the entry for the last time in a pair of fields called respectively post_modified and post_modified_gmt.

post_modified and post_modified_gmt

The first field, post_modified, stores the date and the time of the last modification of the entry according to the TimeZone where the web server is placed, ie, if you live in california, well, if your web server is located in california (and because now we are in august), WP will stores the date and time according to the PDT (Pacific Daylight Time, UTC/GMT-7), and if your web server is located in Adelaide, the field will contain the time in Australia, ACST (Australian Central Standard Time UTC/GMT+9:30).

For the contrary, the second field, post_modified_gmt, in both cases California and Adelaide, will always store the same date and time, the GMT/UTC timezone ( the UTC+0 ).

An example of these two fields for a regular post in wp_posts table. See the difference between fields.

As you see in the previous image, despite the mySQL type for these fields is DateTime, actually, the informaction is stored as raw text so easily and almost effortlessly, you can use and transform this information in a lot of ways.

Using the Last Updated Date inside wp theme templates

The first approach is very simple: accessing directly to the value of these fields. This method can be used when you just need, for example checking the hour, or verifying the month, etc. To do this, invoke the global object $post.

In this next minimal example, we edit directly one of wp theme template, it could be single.php, and introduce the next code in the place of the template where we want to show the date and time of the last modification of this post. These date and time will appear in its raw format, as it’s stored in the database.

...
global $post;
echo 'Updated on : ' . $post->post_modified;     // Updated on: 2018-08-28 18:56:04
...

The second approach for showing the last updated date is to use these two wp core functions: get_the_modified_date() and get_the_modified_time(). Both of them access the same field and showing respectively its part of Date and Time however, these functions shows this information using the global format specified in wp settings page parameters instead of the raw data format. In my case, these functions will show:

...
// settings parameter for date is m/d/Y
echo get_the_modified_date();  // 08/29/2018 
 
// settings parameter for time is H:i
echo get_the_modified_time();  // 00:43
...

These functions can accept two parameters, the second one is just the post but the first one, the most interesting, lets you to specify the format you want use for the output of the date or time so it’s possible to made up complex outputs.

<?php 
get_the_modified_date( $dateTimeFormat, $post ); 
get_the_modified_time( $dateTimeFormat, $post ); 
?>

Remember that both functions access the same complete information so that, for example, using just get_the_modified_time() is possible to show the complete date. See next example.

...
echo get_the_modified_time('\O\n F jS, Y \a\t H:i');   // On August 29th, 2018 at 00:48
...

Using these two simple techniques, accessing directly the global object $post, or using the functions, you can easily include the last updated date inside or around all your post, and the visitors will know that your information is always up to date.

Including the Last Updated Date using a filter

Finally, if you are not confident to modify your theme templates or simple want to use a plugin, you could, for example, introduce this informaction at the end of any post using the the_conten filter. See next example for recipies.

<?php
...
function show_updated_at_end ( $content ) {
    $updated_date = get_the_modified_time( 'F jS, Y' );
    $updated_time = get_the_modified_time( 'H:i' ); 
    $content .= '<p>This recipe has been (re)flawoured on '. $updated_date . ' at '. $updated_time .'</p>';  
    return content;
} 
if ( $post->post_type === 'recipe' )
    add_filter( 'the_content', 'show_updated_at_end' );

And the last update date will be included at the end of your recipies… or wp posts or whatever you want.

Have a nice WordPressing!

Leave a Comment

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>