Using WordPress ‘get_lastpostdate()’ PHP function

The get_lastpostdate() WordPress PHP function retrieves the most recent time that a post on the site was published.

Usage

get_lastpostdate($timezone, $post_type);

Parameters

  • $timezone (string) (Optional) – The timezone for the timestamp. Accepts ‘server’, ‘blog’, or ‘gmt’. ‘server’ uses the server’s internal timezone. ‘blog’ uses the post_date field, which proxies to the timezone set for the site. ‘gmt’ uses the post_date_gmt field. Default ‘server’.
  • $post_type (string) (Optional) – The post type to check. Default ‘any’.

More information

See WordPress Developer Resources: get_lastpostdate()

Examples

Get the last post date using server timezone

This example retrieves the last post date using the server’s internal timezone.

$last_post_date = get_lastpostdate();
echo "Last post date: " . $last_post_date;

Get the last post date using blog timezone

This example retrieves the last post date using the timezone set for the site.

$last_post_date = get_lastpostdate('blog');
echo "Last post date: " . $last_post_date;

Get the last post date using GMT timezone

This example retrieves the last post date using the GMT timezone.

$last_post_date = get_lastpostdate('gmt');
echo "Last post date: " . $last_post_date;

Get the last post date for a custom post type

This example retrieves the last post date for a custom post type called ‘product’.

$last_post_date = get_lastpostdate('server', 'product');
echo "Last product post date: " . $last_post_date;

Get the last post date for multiple post types

This example retrieves the last post date for multiple post types ‘post’ and ‘page’.

$last_post_date = get_lastpostdate('server', array('post', 'page'));
echo "Last post or page date: " . $last_post_date;