Make Scheduled Posts Update The Modified Time in WordPress
We all know how WordPress has two dates/times it saves for posts, right? The Publish date, and the Last Modified date. That way you can track when you originally published the page and when you last changed it.
Well it has always bugged me how scheduled posts didn’t schedule the modified date too. It kind of rats you out by telling the world that you modified it earlier than when you published it.
For example, let’s say you wrote an article today (Saturday August 23) and wanted to schedule it for the upcoming Monday, in 2 days time (Monday August 25th). Here’s what it would show on Monday:
- Modified Date: Saturday August 23rd
- Published Date: Monday August 25th
This might not bother you, but what if you didn’t want the world to know you wrote the post on a Saturday? You scheduled it for Monday – shouldn’t the date show Monday? Isn’t it weird that you are showing you last modified the post before the date you showed it was actually written? What about SEO implications?
Anyway, I wrote some code to fix this situation. Now if you schedule a post for Monday, it will show that it was fully created on Monday:
- Modified Date: Monday August 25th
- Published Date: Monday August 25th
Note: This only works for scheduled posts where you’re scheduling it to publish in the future.
Just drop this at the bottom of your functions.php file in your theme (/wp-content/themes/your-theme-name/functions.php:
/**
* When a scheduled post is published, copy its post_date to post_modified.
*/
function update_modified_date_to_post_date( $post_id, $post ) {
// Just in case this fires for something that isn't a post.
if ( empty( $post ) || $post->post_type !== 'post' ) {
return;
}
wp_update_post( [
'ID' => $post_id,
'post_modified' => $post->post_date,
'post_modified_gmt' => $post->post_date_gmt,
] );
}
add_action( 'future_to_publish', 'update_modified_date_to_post_date', 10, 2 );
Did this help you? Do you have questions for me? Please let me know in the comments!
Comments