[wp-trac] [WordPress Trac] #44597: Scheduling posts adds wrong seconds as post_date

WordPress Trac noreply at wordpress.org
Wed Jan 15 07:26:28 UTC 2025


#44597: Scheduling posts adds wrong seconds as post_date
--------------------------+------------------------------
 Reporter:  katsar0v      |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Date/Time     |     Version:  4.9.7
 Severity:  normal        |  Resolution:
 Keywords:  close         |     Focuses:
--------------------------+------------------------------

Comment (by davidtheplumber):

 This behavior occurs because WordPress automatically adds the current
 seconds from the server time when a post is scheduled, even if only hours
 and minutes are specified. To fix this, you can use the save_post hook to
 override the seconds value in post_date. Here's a solution:

 {{{#!php
 <?php
 add_action('save_post', function($post_id) {
     $post = get_post($post_id);
     if ($post->post_status === 'future') {
         global $wpdb;
         $corrected_date = date('Y-m-d H:i:00',
 strtotime($post->post_date));
         $wpdb->update(
             $wpdb->posts,
             ['post_date' => $corrected_date, 'post_date_gmt' =>
 get_gmt_from_date($corrected_date)],
             ['ID' => $post_id]
         );
         clean_post_cache($post_id);
     }
 });
 }}}

 This ensures that the post_date and post_date_gmt fields have seconds set
 to 00.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/44597#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list