The default behavior for WordPress is to update the modified date when changes to the post are made through (1) the post editor, (2) the quick edit form, or (3) wp_update_post().
Updating the status of a post or the post tags or categories are not a justification for updating the modified date in my opinion. Conversely, updating the post content or title should constitute a date update because it lets the reader know that the digestible portion of the post (the title and content) have been updated.
The function below will prevent WordPress from updating the modified date if there were no changes made to the post content or post title. The function works for changes made in the post editor, custom frontend forms, or the WordPress mobile app.
Here is what the function does:
- Exits if
post_modified
orpost_modified_gmt
are not set (This means the post type does not support the modified date and there is nothing to do because the date will not get updated, regardless) - Checks if the post’s content and title have been changed. If no changes have been made to the post content or title, the function changes the modified date to how it was before the update.
Unlike my previous solution on updating a WordPress post without updating the modified date using wp_update_post(), this function makes it easy by not using remove_filter( 'wp_insert_post_data', 'do_not_update_modified_date', 10, 2 )
.
To use this function, simply place it in your functions.php
file, and forget that it exists.
A Function to Stop WordPress from Updating the Modified Date on Posts
// prevent wordpress from updated the modified date
add_filter( 'wp_insert_post_data', 'do_not_change_modified_date', 10, 2 );
function do_not_change_modified_date( $data, $postarr ) {
// return if post ID is zero
// this indicates that a new post is being processed
if( ((int)$postarr['ID'] === 0) || !$postarr['ID']) return $data;
// get the post
$post_befor_update = get_post($postarr['ID']);
// return if the modified date is not set
// this happens in revisions and can heppen in other post types
if( !isset($data['post_modified']) ||
!isset($data['post_modified_gmt']) ||
!isset($post_befor_update->post_modified) ||
!isset($post_befor_update->post_modified_gmt)) {
return $data;
}
// return if changes were made to the content or title
if( (wp_unslash($data['post_content']) !== $post_befor_update->post_content) ||
(wp_unslash($data['post_title']) !== $post_befor_update->post_title)) return $data;
// change the modified date back to how it was before the update
$data['post_modified'] = $post_befor_update->post_modified;
$data['post_modified_gmt'] = $post_befor_update->post_modified_gmt;
return $data;
}
I use this function on this site, and I can change a post’s status, edit tags and categories, and make any other updates not involving the content and title and the modified date will not get changed.
Hope that helped!