Getting the publication date of a post in WordPress using get_the_date

The get_the_date function in WordPress retrieves the date of the current post or a specified post. This can be useful for displaying the date of a post in a custom format or for creating custom templates for displaying posts based on their date.

By using the get_the_date function, developers can easily access and display the date of a post without having to manually retrieve and format the date themselves. This can save time and reduce the amount of code needed to display post dates on a WordPress website.

Parameters Accepted by get_the_date Function

  • $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.

Return Value of get_the_date Function

The function returns either a string, an integer, or false, depending on whether it successfully retrieves the date the current post was written or encounters a failure.

Examples

How to get the post date in WordPress

Below is an example of using the get_the_date function to display the post date in WordPress:

<?php
$date = get_the_date();
echo $date;
?>

This code snippet retrieves the date of the current post using the get_the_date function and then displays it using the echo statement.

How to get the post date in a specific format

Here’s an example of using the get_the_date function to display the post date in a specific format:

<?php
$date = get_the_date('F j, Y');
echo $date;
?>

In this code snippet, the get_the_date function is used with the ‘F j, Y’ format parameter to retrieve the date of the current post in the format of “Month day, Year”. The date is then displayed using the echo statement.

How to conditionally display the post date

Here’s an example of using the get_the_date function within a conditional statement to display the post date only if it’s not today:

<?php
$post_date = get_the_date('Y-m-d');
$current_date = date('Y-m-d');

if ($post_date !== $current_date) {
 echo 'Posted on: ' . get_the_date();
}
?>

In this code snippet, the get_the_date function is used to retrieve the date of the current post, and then a conditional if statement is used to check if the post date is not equal to today’s date. If the condition is met, the post date is displayed using the echo statement.

Conclusion

The get_the_date function is an useful component for displaying the date of a post in WordPress. Its flexibility and customization options make it a valuable asset for developers and website administrators. By using this function, users can easily retrieve and display the date of a post in a format that suits their specific needs.

With the ability to specify the date format, post ID, and display options, the get_the_date function offers a high level of control and precision. Whether it’s for a blog, news website, or any other type of content-driven platform, this function provides a reliable way to present accurate and relevant date information to users.

The get_the_date function is a valuable addition to the WordPress developer’s toolkit, offering a seamless way to manage and display post dates with ease and precision.

Related WordPress Functions