WP-Mix

A fresh mix of code snippets and tutorials

WordPress use post modified time for better SEO

Quick collection of code snippets to help customize post dates to reflect most-recent-update information. I use this technique here at WP-Mix, DigWP, and elsewhere. Using the most recent date as the post date can boost the SEO value of your site.

Displaying Publish Dates?

If you’re using something like this to display your post dates:

<time datetime="<?php the_time('Y-m-j'); ?>"><?php the_time('F jS, Y'); ?></time>

<time datetime="<?php the_time('Y-m-j'); ?>" pubdate><?php the_time('l, F jS, Y'); ?></time>

..you are limiting the post date to the day that the post was originally published. This is fine for many situations, but if you ever update your WP posts with newer information, it can be an SEO benefit to display the date that reflects the most recent update. You know, so if you publish a post in 2012 and then update it in 2018, the post date will show the most current date instead of the older date. Whether or not this strategy is a good fit for your site is entirely up to you; I’m just here to show you how it’s done.

Displaying Update Dates!

So to display the most recent post date that is available (either the original publish date or the update date), you can use something like this in your theme template (in the WP Loop):

<time datetime="<?php the_modified_time('c'); ?>"><?php the_modified_time('F jS, Y'); ?></time>

What’s going on here? Well basically we replaced the the_time() tag the_modified_time(). That’s the moral of the story, right there. When using this code, your posts will display the most current date available.

Note: Check out the official WordPress guide on customizing the date/time format. It’s just good times all around.

Some examples..

To help understand how this technique might be applied in your theme template, here are a couple of “real life” examples:

<?php if (get_the_time('c') === get_the_modified_time('c')) : ?> <span>on</span> <?php else : ?> • <span>Updated on</span> <?php endif; ?> <time datetime="<?php the_modified_time('c'); //the_time('Y-m-j'); ?>"><?php the_modified_time('F jS, Y'); ?></time>

This snippet can be seen at DigWP, just check out the metadata for any post. What it does? Simple: it checks the post date and displays something like this if the post has not been updated:

Posted by Jeff Starr on January 3rd, 2017

Otherwise, if the post has been updated, something like this is displayed:

Posted by Jeff Starr • Updated on January 9th, 2017

So the snippet provides a way to display different text/markup based on what’s available for the post date. Here is another example:

<?php if (get_the_time('c') === get_the_modified_time('c')) : ?> Post Date: <?php else : ?> Updated: <?php endif; ?>
<time datetime="<?php the_modified_time('c'); ?>"><?php the_modified_time('l, F jS, Y'); ?></time>

This snippet is used here at WP-Mix. It does the same thing as the previous example: displays output conditionally, based on the post date. To see it in action, browse through some posts here on the site :)

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress