How to prevent WordPress from updating the modified time?

Sometimes I want to change a post a little bit, but the post was already published on my blog. The change concerns adding/removing tags or rewriting the title of the post (just to correct a misspelled word). All the things can be done using the admin panel by pressing "quick edit".

Unfortunately, the action updates the "modified time". I'm using the "modified time" so people would know whether a post was modified or not. But I want to disable the modified time updates in the case when I just make some small changes like to ones described above. Is there a way to do it?

Topic title tags posts Wordpress

Category Web


I tried out the answers above and the similar solution found here but was running into a problem where draft posts were still having their "Last Modified" date updated to the current time even though the published posts were updating correctly.

On our site, that was a catastrophic problem because we have thousands of draft posts saved and being updated caused them to all be pushed to the top of the post list by default, crowding out all the published posts that had been updated recently.

After restoring a backup of our site, I looked through the wp_update_post documentation but wasn't able to find much about the problem.

After some trial and error, I came up with this workaround code that successfully updates both draft and published posts without changing their "Last Modified" date. I'm not sure if all of the code below is necessary, but at least it seems to work.

function do_not_update_modified_date($new, $old)
{
    $post_status = $new['post_status'];
    
    if ($post_status === 'draft' || $post_status === 'pending' || $post_status === 'auto-draft')
    {
        $new_post_modified      = $old['post_modified'];
        
        if ((!empty($new_post_modified)) && ($new_post_modified !== '0000-00-00 00:00:00'))
        {
            $new_post_modified = substr($new_post_modified, 0, -1) . (intval(substr($new_post_modified, -1)) + 1) % 10;
        }
        
        $new['post_modified']      = $new_post_modified;
        $new['post_modified_gmt']  = $new_post_modified;
        
    }
    
    else
    {
        $new['post_date']           = $old['post_date'];
        $new['post_date_gmt']       = $old['post_date_gmt'];
        $new['post_modified']       = $old['post_modified'];
        $new['post_modified_gmt']   = $old['post_modified_gmt'];
    }
    
    return $new;
}

add_filter('wp_insert_post_data', 'do_not_update_modified_date'], 1, 2);
wp_update_post(['edit_date' => true, 'ID' => $post_id, 'post_name' => $new_post_name, 'post_title' => $new_post_title,]);
remove_filter('wp_insert_post_data', 'do_not_update_modified_date', 1, 2);

Warning: I haven't tested this on anything besides published posts and draft posts. I have no idea whether this will mess up any scheduled posts or create any other problems. Please take a backup before running this and check everything thoroughly afterwards.


Although that doesn't exactly answer the question, I leave 30 minutes before showing the modified date to correct a few errors quickly.

<?php if ( get_the_modified_time( 'U' ) > get_the_time( 'U' ) + 1800 ) : ?>
    <div class="update"><?php the_modified_time( 'F j, Y g:i a' ); ?></div>
<?php endif; ?>

Here, it is the delay + 1800 (1800 secs = 30 mins) that makes the difference.


The question of how to customize which post data gets updated has been answered elsewhere on StackExchange.

Here is a specific example of how to stop the modified date from being updated:

function stop_modified_date_update( $new, $old ) {
    $new['post_modified'] = $old['post_modified'];
    $new['post_modified_gmt'] = $old['post_modified_gmt'];
    return $new;
}

add_filter( 'wp_insert_post_data', 'stop_modified_date_update', 10, 2 );

// do stuff that updates post(s) here

remove_filter( 'wp_insert_post_data', 'stop_modified_date_update', 10, 2 );

NB: It is extremely important to remove the filter when you're done with your special tasks unless you really want the modified date to never update anywhere on the site.

Happy coding!


It sounds like you're only displaying this information on the single post view and not making any searches for it.

So another approach could be to save the last modified date-time for the content changes in a hidden post meta field, e.g. _wpse_content_modified.

Look e.g. into the post_updated hook where you've access to both the previous post object and the modified post object.

That way you don't have to make irreversible data modifications, for a core functionality, that you might later want to use unchanged.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.