How to set publishing date and time for a WordPress post programmatically?

You can set publish date for posts in the WordPress admin panel but sometimes you may want to set a post publishing date programmatically. for example, when you have lots of unpublished posts and you want to publish these posts on different dates in the future, you can set the publish date for each post programmatically inside a loop (bulk publishing date setting for WordPress posts), using WordPress update post function.

5 from 5
from 3 user
(Rate this post)

The short answer for the “How to set date and time for a WordPress post programmatically” question is here:

$id = 1;
$date_time = "2021-12-31 23:59:59";//[Y-m-d H:i:s]
wp_update_post(
    array (
        'ID' => $id,
        'post_date' => $date_time,
        'post_date_gmt' => get_gmt_from_date( $date_time )
    )
);

The above codes will update a post with the id number of 1 and set the post publishing date.

Here is more explanation if you are interested:

wp_update_post() function can update a post in WordPress, using an array of attributes with value.

You must pass the id of the post you wish to update, using the ID attribute.

You can set post_date and post_date_gmt to change post publishing date to whatever you want.

Proper structure for post_date attribute is Y-m-d H:i:s

how to set publishing date and time for unpublished WordPress posts inside a loop?

What do you think about "How to set publishing date and time for a WordPress post programmatically?"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu