Using WordPress ‘get_the_modified_date’ PHP filter

The get_the_modified_date WordPress PHP filter allows you to modify the date a post was last updated.

Usage

add_filter('get_the_modified_date', 'your_custom_function', 10, 3);
function your_custom_function($the_time, $format, $post) {
    // your custom code here
    return $the_time;
}

Parameters

  • $the_time (string|int|false) – The formatted date or false if no post is found.
  • $format (string) – PHP date format.
  • $post (WP_Post|null) – WP_Post object or null if no post is found.

More information

See WordPress Developer Resources: get_the_modified_date

Examples

Change date format

Change the modified date format to a custom format.

add_filter('get_the_modified_date', 'change_modified_date_format', 10, 3);
function change_modified_date_format($the_time, $format, $post) {
    $new_format = 'F j, Y';
    $the_time = get_post_modified_time($new_format, false, $post, true);
    return $the_time;
}

Append custom text

Add custom text before the modified date.

add_filter('get_the_modified_date', 'append_custom_text', 10, 3);
function append_custom_text($the_time, $format, $post) {
    $custom_text = 'Last updated on: ';
    return $custom_text . $the_time;
}

Add custom timezone

Display the modified date in a custom timezone.

add_filter('get_the_modified_date', 'custom_timezone_modified_date', 10, 3);
function custom_timezone_modified_date($the_time, $format, $post) {
    $timezone = 'America/New_York';
    $date_obj = new DateTime(get_post_modified_time('Y-m-d H:i:s', true, $post), new DateTimeZone('UTC'));
    $date_obj->setTimezone(new DateTimeZone($timezone));
    return $date_obj->format($format);
}

Display human-readable date

Show the modified date in a human-readable format, like “2 days ago”.

add_filter('get_the_modified_date', 'human_readable_modified_date', 10, 3);
function human_readable_modified_date($the_time, $format, $post) {
    $modified_time = get_post_modified_time('U', true, $post);
    return human_time_diff($modified_time) . ' ago';
}

Hide modified date for specific posts

Hide the modified date for specific post IDs.

add_filter('get_the_modified_date', 'hide_modified_date_for_specific_posts', 10, 3);
function hide_modified_date_for_specific_posts($the_time, $format, $post) {
    $hidden_post_ids = array(1, 2, 3);
    if (in_array($post->ID, $hidden_post_ids)) {
        return '';
    }
    return $the_time;
}