Updating WordPress Post Modified Dates

The standard way of updating post values in WordPress is to use wp_update_post(). For example:

wp_update_post( [
    'ID'         => 30,
    'post_title' => 'New Title',
] );

But you can’t update a post’s post_modified / post_modified_gmt values in this way; wp_update_post() calls wp_insert_post(), which sets these values in a way that can’t be overridden .

The effect is a catch-22: You can modify the post_modified values with that function, but that function then modifies those values again to record the fact that you modified those values.

The workaround is to update the values via the database directly. Here’s a handy snippet I put together for doing that:

 * Sets the post_modified and post_modified_gmt values for the specified post.
 * @author George Gecewicz <[email protected]>
 * @since 2019-09-10
 * @param int    $post_id ID of the post whose values will be updated.
 * @param string $time_to_set The value of post_modified to set.
 * @param string $time_to_set_gmt Optional value of post_modified_gmt to set; if not set, $time_to_set will be used.
 * @return int|bool Number of rows affected. Boolean false on error.
function set_modified_time_for_post( int $post_id, string $time_to_set, string $time_to_set_gmt = '' ) {
    global $wpdb;

    return $wpdb->query(
            "UPDATE $wpdb->posts
            SET post_modified = %s, post_modified_gmt = %s
            WHERE ID = %d",
            $time_to_set_gmt ?: $time_to_set,

So to set the post_modified time of a post with the ID of 30, you’d simply call that function like this:

$a_day_ago = date( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS );

set_modified_time_for_post( 30, $a_day_ago );

Leave a Reply

Your email address will not be published. Required fields are marked *