Getting a post last updated date using get_the_modified_date in WordPress

The get_the_modified_date function in WordPress retrieves the date the post was last modified. This can be useful for displaying the most recent update to a post, allowing users to see when the content was last edited.

By using get_the_modified_date, developers can easily access and display the modification date of a post without having to manually retrieve and format the information themselves.

  • It can be used to dynamically display the last modified date on a post or page.
  • It can be used to create custom templates that show the modification date in a specific format.

Parameters Accepted by get_the_modified_date Function

The get_the_modified_date function accepts the following parameters:

  • $format (string, optional): This parameter represents the PHP date format. If not provided, it defaults to the ‘date_format’ option.
  • $post (int|WP_Post, optional): This parameter represents the Post ID or WP_Post object. If not provided, it defaults to the current post.

Value Returned by get_the_modified_date Function

The get_the_modified_date function returns the following:

A string, integer, or false value representing the date the current post was modified. It returns false on failure.

Examples

How to get the modified date of a post

Use the get_the_modified_date function to retrieve the modified date of a post.

$post_modified_date = get_the_modified_date();
echo 'Last modified date: ' . $post_modified_date;

How to display the modified date in a specific format

Use the get_the_modified_date function with a date format parameter to display the modified date in a specific format.

$post_modified_date = get_the_modified_date('F j, Y');
echo 'Last modified date: ' . $post_modified_date;

How to check if a post has been modified

Use the get_the_modified_date function and an if statement to check if a post has been modified.

if (get_the_modified_date() !== get_the_date()) {
 echo 'This post has been modified';
} else {
 echo 'This post has not been modified';
}

Conclusion

The get_the_modified_date function provides a useful way to retrieve the modified date of a post in WordPress. By using this function, developers can easily display the last modified date of a post on their website, providing users with valuable information about the currency of the content. Additionally, the function offers flexibility in terms of formatting the date output, allowing for customization to suit the specific needs of the website.

With its straightforward usage and customizable options, the get_the_modified_date function is a valuable tool for WordPress developers looking to enhance the user experience and provide accurate information to their audience. By incorporating this function into their projects, developers can ensure that their content remains relevant and up-to-date, ultimately improving the overall quality of their websites.

Related WordPress Functions