Using WordPress ‘get_lastpostmodified’ PHP filter

The get_lastpostmodified WordPress PHP filter retrieves the most recent time a post on the site was modified.

Usage

add_filter('get_lastpostmodified', 'your_custom_function', 10, 3);

function your_custom_function($lastpostmodified, $timezone, $post_type) {
    // your custom code here
    return $lastpostmodified;
}

Parameters

  • $lastpostmodified (string|false) – The most recent time that a post was modified, in ‘Y-m-d H:i:s’ format. False on failure.
  • $timezone (string) – Location to use for getting the post modified date. See get_lastpostdate() for accepted $timezone values.
  • $post_type (string) – The post type to check.

More information

See WordPress Developer Resources: get_lastpostmodified

Examples

Display the last post modified date in a different format

add_filter('get_lastpostmodified', 'display_last_modified_date', 10, 3);
function display_last_modified_date($lastpostmodified, $timezone, $post_type) {
    if ($lastpostmodified) {
        $lastpostmodified = date('F j, Y, g:i a', strtotime($lastpostmodified));
    }
    return $lastpostmodified;
}

Update last post modified date when a specific post type is modified

add_filter('get_lastpostmodified', 'update_last_modified_date', 10, 3);

function update_last_modified_date($lastpostmodified, $timezone, $post_type) {
    if ($post_type === 'product') {
        $lastpostmodified = current_time('mysql', 0);
    }
    return $lastpostmodified;
}

Exclude a specific post type from last post modified date calculation

add_filter('get_lastpostmodified', 'exclude_post_type', 10, 3);

function exclude_post_type($lastpostmodified, $timezone, $post_type) {
    if ($post_type === 'private_post') {
        return false;
    }
    return $lastpostmodified;
}

Display the last post modified date in the local time zone

add_filter('get_lastpostmodified', 'display_local_time', 10, 3);

function display_local_time($lastpostmodified, $timezone, $post_type) {
    if ($lastpostmodified) {
        $lastpostmodified = get_date_from_gmt($lastpostmodified, 'Y-m-d H:i:s');
    }
    return $lastpostmodified;
}

Return the last post modified date for a custom post type only

add_filter('get_lastpostmodified', 'custom_post_type_modified', 10, 3);

function custom_post_type_modified($lastpostmodified, $timezone, $post_type) {
    if ($post_type === 'custom_post_type') {
        return $lastpostmodified;
    }
    return false;
}