Brogramo
Guest
Guest

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

WordPress will update the modified date when you update the post status or make minor edits. This post will show you how to use a filter to prevent WordPress from updating the modified date.

wp_insert_post() is the culprit

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 || '-- ::' === $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', 1, 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', 1, 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.”