The get_lastpostdate WordPress PHP filter retrieves the most recent time a post was published on the website.
Usage
add_filter('get_lastpostdate', 'your_function_name', 10, 3); function your_function_name($lastpostdate, $timezone, $post_type) { // your custom code here return $lastpostdate; }
Parameters
$lastpostdate
(string|false) – The most recent time a post was published, in ‘Y-m-d H:i:s’ format. False on failure.$timezone
(string) – Location to use for getting the post published date. Seeget_lastpostdate()
for accepted$timezone
values.$post_type
(string) – The post type to check.
More information
See WordPress Developer Resources: get_lastpostdate
Examples
Modify the last post date for specific post type
Adjust the last post date for the ‘news’ post type by adding a custom offset:
add_filter('get_lastpostdate', 'modify_lastpostdate_for_news', 10, 3); function modify_lastpostdate_for_news($lastpostdate, $timezone, $post_type) { if ($post_type === 'news') { $offset = 1 * DAY_IN_SECONDS; // offset by 1 day $lastpostdate = date('Y-m-d H:i:s', strtotime($lastpostdate) - $offset); } return $lastpostdate; }
Display last post date in a different timezone
Convert the last post date to the ‘America/New_York’ timezone:
add_filter('get_lastpostdate', 'convert_lastpostdate_timezone', 10, 3); function convert_lastpostdate_timezone($lastpostdate, $timezone, $post_type) { if ($timezone === 'America/New_York') { $date = new DateTime($lastpostdate, new DateTimeZone('UTC')); $date->setTimezone(new DateTimeZone($timezone)); $lastpostdate = $date->format('Y-m-d H:i:s'); } return $lastpostdate; }
Ignore specific post types
Ignore ‘attachment’ and ‘revision’ post types from the last post date calculation:
add_filter('get_lastpostdate', 'ignore_specific_post_types', 10, 3); function ignore_specific_post_types($lastpostdate, $timezone, $post_type) { if (in_array($post_type, array('attachment', 'revision'))) { return false; } return $lastpostdate; }
Add custom text to the last post date
Append custom text to the last post date:
add_filter('get_lastpostdate', 'add_custom_text_to_lastpostdate', 10, 3); function add_custom_text_to_lastpostdate($lastpostdate, $timezone, $post_type) { return $lastpostdate . ' (Last Published)'; }
Format the last post date differently
Change the format of the last post date to ‘d/m/Y H:i’:
add_filter('get_lastpostdate', 'change_lastpostdate_format', 10, 3); function change_lastpostdate_format($lastpostdate, $timezone, $post_type) { $date = DateTime::createFromFormat('Y-m-d H:i:s', $lastpostdate); return $date->format('d/m/Y H:i'); }