Using get_post_modified_time to get a post last update time in WordPress

The WordPress get_post_modified_time function retrieves the time at which a particular post was last modified. This function can be utilized to display the date and time of the last modification made to a post. It is designed to provide information about the post’s revision history, which can be beneficial for tracking changes and updates made to the content.

The output of the get_post_modified_time function can be formatted to meet specific requirements. It can provide the modification time in different formats such as ‘U’ for Unix timestamp or ‘G’ for Greenwich Mean Time. The function can also return the modification time in the local time of the blog or in GMT.

When the get_post_modified_time function is used in a WordPress theme, it allows the theme to display the last modified time of each post. This can be useful for visitors who want to know when the content of the post was last updated. It also provides transparency about the freshness of the content on the blog or website.

The get_post_modified_time function serves as a method for retrieving and presenting the last modification time of a post in WordPress.

Parameters Accepted by the get_post_modified_time Function

The get_post_modified_time function in WordPress accepts several parameters that can be used to modify its behavior. Each of these parameters is optional, and each has a default value.

  • $format (string) – This parameter determines the format for the time the post was last modified. It can accept ‘G’, ‘U’, or any PHP date format. If not specified, it defaults to ‘U’.
  • $gmt (boolean) – This parameter defines whether to fetch the Greenwich Mean Time (GMT). By default, it is set to false.
  • $post (int|WP_Post) – This parameter can either be a post ID or a post object. If not provided, it defaults to the global $post object.
  • $translate (boolean) – This parameter determines whether the time string should be translated. It is set to false by default.

Return Value of the get_post_modified_time Function

The get_post_modified_time function returns a formatted date string or a Unix timestamp if the $format parameter is set to ‘U’ or ‘G’. In case of failure, it returns false.

Examples

How to Display the Last Modified Time of a Post

$post_id = 123; // replace with your post ID
$format = 'F j, Y, g:i a'; // replace with your preferred date format
$gmt = false; // replace with true if you want GMT time

$modified_time = get_post_modified_time($format, $gmt, $post_id);
if ($modified_time) {
 echo "<p>Last modified on: " . $modified_time . "</p>";
} else {
 echo "<p>Could not retrieve modified time.</p>";
}

This snippet retrieves and displays the last modified time of a post with a specific ID. The time is formatted according to the specified format string. If the function fails to retrieve the modified time, a failure message is displayed.

How to Compare the Modified Time of Two Posts

$post_id1 = 123; // replace with your first post ID
$post_id2 = 456; // replace with your second post ID

$modified_time1 = get_post_modified_time('U', false, $post_id1);
$modified_time2 = get_post_modified_time('U', false, $post_id2);

if ($modified_time1 && $modified_time2) {
 if ($modified_time1 > $modified_time2) {
 echo "<p>Post $post_id1 was modified more recently than post $post_id2.</p>";
 } else if ($modified_time1 < $modified_time2) {
 echo "<p>Post $post_id2 was modified more recently than post $post_id1.</p>";
 } else {
 echo "<p>Both posts were modified at the same time.</p>";
 }
} else {
 echo "<p>Could not retrieve modified times.</p>";
}

This snippet retrieves the Unix timestamps of the last modifications of two posts and compares them. It then displays a message indicating which post was modified more recently, or if they were modified at the same time. If the function fails to retrieve one or both of the modified times, a failure message is displayed.

How to Display the GMT Modified Time of a Post

$post_id = 123; // replace with your post ID
$format = 'F j, Y, g:i a'; // replace with your preferred date format

$gmt_modified_time = get_post_modified_time($format, true, $post_id);
if ($gmt_modified_time) {
 echo "<p>Last modified on (GMT): " . $gmt_modified_time . "</p>";
} else {
 echo "<p>Could not retrieve GMT modified time.</p>";
}

This snippet retrieves and displays the last modified time of a post in GMT. The time is formatted according to the specified format string. If the function fails to retrieve the GMT modified time, a failure message is displayed.

Conclusion

The get_post_modified_time function in WordPress is a tool that retrieves the time at which a specific post was last modified. This function can be utilized in a variety of instances where tracking the modification time of posts is necessary. For instance, it can be used to provide readers with the most recent update time of a post, or it can be used programmatically to perform operations based on the last modification time of posts. It is an important function that enhances the dynamic capabilities of WordPress in managing and presenting content.

Related WordPress Functions