Brogramo
<span class='guest_name'>Guest</span>Guest

How to Update a WordPress Post Without Updating the Modified Date Using wp_update_post()

You want to update a post without updating the modified date because you probably feel that the edits you made do not justify a date change, or maybe you just want to change the post’s status. Not to worry, there is a solution.

Before we start, I want to bring to your attention that you cannot use wp_update_post() without using a filter to achieve your goal. This is how you might think wp_update_post() is a good way to keep the modified date unchanged.

Assuming that we deemed it unnecessary to update the modified date when we are only updating the post’s tags, categories, or other post meta.

// get the post
$post = get_post($post_ID);

// set post data
$postarr['ID'] = $post->ID;
$postarr['tags_input'] = $tags; // an array of tags
$postarr['post_modified'] = $post->post_modified;
$postarr['post_modified_gmt'] = $post->post_modified_gmt;

// update default post
$update_post = wp_update_post($postarr);

The logic behind this code is to update the post_modified and post_modified_gmt with the post’s original date, thus keeping the modified date unchanged.

This all makes sense, but the problem lies in wp_insert_post(). Here is how:

wp_update_post() calls wp_insert_post() and wp_inser_post() makes sure that the modified date is set to the current time when the post is being updated.

if ( $update || '0000-00-00 00:00:00' === $post_date ) {
$post_modified     = current_time( 'mysql' );
$post_modified_gmt = current_time( 'mysql', 1 );
}

With that being said, let us explore an option for updating a post without updating its modified date using a WordPress filter.

How to Update a WordPress Post Without Updating the Modified Date Using a Filter

The filter we will use to help us bypass WordPress’s default behavior of updating the modified date when the post is updated is wp_insert_post_data.

wp_insert_post_data is called after WordPress updates the modified date and we will reverse WordPress’ default behavior.

Example:

// filter to prevent wordpress from updated modified date

add_filter( 'wp_insert_post_data', 'do_not_update_modified_date', 10, 2 );         
function do_not_update_modified_date( $data, $postarr ) {
    
    // 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($postarr['post_modified'])   || 
        !isset($postarr['post_modified_gmt'])) {
        return $data;
    }
    
    // change the modified date back to how it was before the update

    $data['post_modified']      = $postarr['post_modified'];
    $data['post_modified_gmt']  = $postarr['post_modified_gmt'];
    
    return $data;
}

$postarr['ID'] = $post->ID; // post ID being updated
$postarr['tags_input'] = $tags; // array of tags (Or it can be something like changing the post's status)

// update post
$update_post = wp_update_post($postarr);

// remove filter after post has been updated
remove_filter( 'wp_insert_post_data', 'do_not_update_modified_date', 10, 2 );

When you update a post using wp_updae_post() in conjunction with wp_insert_post_data, the modified date will remain unchanged.

According to WordPress, “To remove a hook, the $callback and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.”