Do you need to get the publish date for the active post? or just get the publish date by specifying a post ID? in this WordPress tutorial, we used a particular WP function to retrieve the publish date of any post with various formats. We used PHP codes to get the post date of a requested post, but it can be wrapped into a shortcode.
There is a function in WordPress that can retrieve a post’s publish date.
get_the_date()
is the function responsible for getting the publish date of a post.
get_the_date()
accepts an optional ‘format’ parameter, which is helpful when you want to customize the output string.
If you do not specify the date format, get_the_date() will get it from WordPress settings (Path: Admin Panel -> Settings -> General).
Get the Active Post Publish Date in WordPress
To retrieve the Publish Date of the current queried post in WordPress, use this as an example:
$postDate = get_the_date();
//printing the active post date with WP setting's format:
echo $postDate;
To specify the date format, use this example for Active Post:
//Storing a few possible date formats in an array:
$postDateFormats = [
'F j, Y', // ex: September 1, 2022
'jS M Y', // ex: 1st Sep 2022
'Y/m/d g:i:s A', // ex: 2022/09/01 6:09:06 AM
'Y/m/d \a\t g:i A', // ex: 2022/09/01 at 6:09 AM
'Y/m/d g:i:s A', // ex: 2022/09/01 6:09:06 AM
'c', // ex: 2022-09-01T06:09:06-04:00(ISO format for schema)
'r', // ex: Thu, 01 Sep 2022 06:09:06 -0400 (RFC 2822)
'u', // ex: 1662027259 (Unix timestamp)
];
//print post date using them in different lines:
foreach($postDateFormats as $format){
echo get_the_date($format)."
";
}
The above example used a loop to go through each post format and print the date.
You can select one of them and use it; for example, to use September 1, 2022
output format, use: get_the_date('F j, Y')
.
Get the Post Publish Date for Specified Post ID in WordPress
To specify the post ID, Use the second parameter of the get_the_date
function.
For example, to get the publish date for a post with ID = 1
, Use this code:
$postId = 1;
$postDate = get_the_date('',$postId);
//printing the post date with WP setting's format:
echo $postDate;
To specify the date format and the Post ID at the same time, use this example (we used post ID = 1):
//define post ID you want the date for:
$postId = 1;
//Storing a few possible date formats in an array:
$postDateFormats = [
'F j, Y', // ex: September 1, 2022
'jS M Y', // ex: 1st Sep 2022
'Y/m/d g:i:s A', // ex: 2022/09/01 6:09:06 AM
'Y/m/d \a\t g:i A', // ex: 2022/09/01 at 6:09 AM
'Y/m/d g:i:s A', // ex: 2022/09/01 6:09:06 AM
'c', // ex: 2022-09-01T06:09:06-04:00(ISO format for schema)
'r', // ex: Thu, 01 Sep 2022 06:09:06 -0400 (RFC 2822)
'u', // ex: 1662027259 (Unix timestamp)
];
//print post date using them in different lines:
foreach($postDateFormats as $format){
echo get_the_date($format, $postId)."
";
}
The above example used a loop to go through each post format and print the date for the specified post.
You can select one of them and use it; for September 1, 2022
output format, use: get_the_date('F j, Y', $postId)
.
Printing the Post Publish Date Using a Shortcode
To print the publish date with a shortcode, first, we have to define the shortcode and then use it in the WordPress editor.
Step 1: Register the [my-post-date]
Shortcode in WordPress
open the functions.php
file of your active theme or your custom-made plugin main PHP file, and copy/paste this snippet into it:
Step 2: Use the [my-post-date]
Shortcode in WordPress
Open a post or create a new one. in the editor, just write [my-post-date]
in the content part. This will print the active post date with the default format on that post page.
If you want to print the date in a special format, use the post-format
attribute for the shortcode. (Ex: [my-post-date date-format='F j, Y']
)
If you want to print the date for a specific post ID, use the post-id
attribute for the shortcode. (Ex: [my-post-date date-format='F j, Y' post-id=1]
)
written by Mehdi Nazari about in WordPress WordPress Plugin Development WordPress Theme Development
What do you think about "How to Get Post Date with Custom Format in WordPress [Shortcode]"?