2

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?

1
  • Are you using revisions?
    – birgire
    Oct 23, 2016 at 15:10

4 Answers 4

5

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!

0

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.

0

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.

2
  • 2
    From the code you have posted I don't think this answers OP question at all, he wanted to update post data, title, meta etc... without it updating the post updated datetime. Your answer doesn't adress this need at all. Oct 20, 2021 at 14:06
  • This answer makes no sense whatsoever. It doesn't answer the OPs question in the slightest, nor does it do what you're hoping it does. It looks like your code snippet is for the front page of a post and not the admin area. Your code snippet doesn't prevent anything. The post itself will still show up with the newly modified version and will show the modified date in any loop such as front page, categories, tags, archives, feeds. If you want to update a post but are afraid it might contain errors such as typos, simply click "preview post" to get a live preview and then fix your typos.
    – Kevin M
    May 20, 2023 at 8:02
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.