11

I suppose getting the first revision of a particular post and get the date, right ?

How to do that?

9
  • 2
    The post_date and post_date_gmt is the date the post was created on. Feb 4, 2015 at 12:41
  • Thank you Pieter, but it seems both give me last modification date. Are you sure ?
    – Max
    Feb 4, 2015 at 12:44
  • 1
    Then you have a custom filter or function that is modifying your post dates. If a post is modified, that specific date and time is saved as post_modified Feb 4, 2015 at 12:46
  • 3
    I am sorry, but i think this is wrong, at least for scheduled posts : post_date is the scheduled date. And there is no filter i directly query database and get the post then call ->post_date. Any idea to get the very first time the post was added to wordpress ?
    – Max
    Feb 4, 2015 at 12:58
  • I've just tested it : i retrieve post_date from a scheduled post, it gives me the scheduled date. Not the same for you ?
    – Max
    Feb 4, 2015 at 13:20

5 Answers 5

10

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

enter image description here

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

4
  • 4
    Thank you, but this is strange there is no such a function
    – Max
    Feb 27, 2015 at 9:32
  • Is such a function really necessary. Your case is very isolated Feb 27, 2015 at 9:41
  • 5
    I don't think the case is very isolated, especially now that woocommerce is in the picture and you want to differentiate between the age of products
    – Kirby
    Jul 15, 2015 at 20:05
  • @Kirby please explain in an answer Jul 15, 2015 at 20:34
4

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).

1
  • I believe this can be simplified by calling 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
    – Tom J Nowell
    Oct 30, 2019 at 22:06
1

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.

1

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. :)

0

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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.