I suppose getting the first revision of a particular post and get the date, right ?
How to do that?
The post_date
and post_date_gmt
serves as the date that the post was created. For scheduled posts this will be the date on which the post is scheduled to be published.
There is no reliable native method to determine the date when a scheduled post was added. For scheduled posts, you can try the post_modified
or post_modified_gmt
dates as this will correspond when the post was first added. You have to remember, this is unreliable as this date will change when a post is modified
Another (unreliable as well) method, is to use revisions. If you have revisions enabled, when a post is scheduled or published, a post revision is saved. That revision, which will be saved after the scheduled post when the scheduled button is clicked, will hold the current post date on which the scheduled button is clicked. Check the pic below
If you need a reliable way of doing this, you will need to write your own custom function to save the exact time when the scheduled button is clicked to schedule a post
I needed the same info and just figured out by myself. You have to make a custom query to get the first _wp_old_date
from the postmeta
table as @dipak_pusti suggested. Run the following statement:
global $wpdb;
$post_id = 1234;
$meta_key = '_wp_old_date';
$post_create_date = $wpdb->get_var( $wpdb->prepare(
"
SELECT MIN($wpdb->postmeta.meta_value) as post_create_date
FROM $wpdb->postmeta
WHERE $wpdb->postmeta.post_id = %d
AND $wpdb->postmeta.meta_key = %s
",
$post_id,
$meta_key
) );
if(is_null($post_create_date)) {
$post_create_date = get_the_date('Y-m-d', $post_id);
}
echo "<p>Post create date is: {$post_create_date}</p>";
Note: If the _wp_old_date
is NULL
(this means the publish date has never changed) you can use the date from get_the_date('Y-m-d', $post_id)
.
get_post_meta( $post_id, '_wp_old_date' );
, making sure not to pass a true
value as the third parameter. The result will be an array of values to check, avoiding the need for an SQL query. As a bonus it uses WP Cache so if you already have a post object no queries are made at all
Use get_the_date();
to get the date defined in the meta box "Publish" at the field "Published on ..", this usually is the date the user created the post unless (s)he change it.
When I was stuck with same type of case as yours, i found a meta key _wp_old_date
with a meta value representing a date.
By Googling this, i found a function is being used to store the old value when a Post or Object is being changed.
https://wpseek.com/function/wp_check_for_changed_dates/
I know, this is very late answer, but it may help others in future. :)
I have wrote a query based on the other answers.
SELECT p.post_title,
(SELECT Min(postmeta.meta_value)
FROM wp_postmeta postmeta
WHERE postmeta.post_id = p.id
AND postmeta.meta_key = '_wp_old_date')
"First publ from postmeta",
DATE(revisions.min_post_modified)
"First edit found in revisions",
DATE(revisions.min_post_date) min_post_date
FROM wp_posts p,
(SELECT post_parent,
Min(pr.post_modified) min_post_modified,
Min(pr.post_date) min_post_date
FROM wp_posts pr
GROUP BY pr.post_parent) revisions
WHERE p.id = revisions.post_parent
post_date
andpost_date_gmt
is the date the post was created on.post_modified