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 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
You can set
post_date_gmt to change the post publishing date to whatever you want.
Proper structure for
If you want to know how to query posts in WordPress using publish date, you can read my tutorial: