How to Set Post Publish Date Programmatically in WordPress

4 from 5
from 9 user
(Rate this post)
How to Set Post Publish Date Programmatically in WordPress

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.

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]
    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 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 the post publishing date to whatever you want.

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

If you want to know how to query posts in WordPress using publish date, you can read my tutorial:

WordPress Query Posts by Category, Tag, Custom attribute, and more!

What do you think about "How to Set Post Publish Date Programmatically in WordPress"?

Your rating submit was successfull.

Your rating submit was not successfull.