This might be kind of funky to pull off, but does anyone have any ideas on how I could automatically set the time to 8am on any newly created post drafts? In both the Classic Editor and the Block Editor?
Edit: The Block Editor uses React; this creates some issues. /
I write a week's worth of scheduled posts at a time, and I change the time to 8am on each so that they are published early and the "new post" emails go out early. It will save a few steps if each time I create a new post, the time is set to 8am. I don't want to set the date, only the time.
I suppose I need to hook into the new post function and use wp_insert_post_data
. Then I can then set the date in the future for a scheduled publication.
I know there are 4 date/time fields: post_date, post_modified, and their GMT variants. Date/time values are in this format: 2024-10-01 12:00:00
I'd also like to be sure that the function to change the time only runs once, on new post creation, so that the user can change the time, if needed, in a future post revision.
This might be kind of funky to pull off, but does anyone have any ideas on how I could automatically set the time to 8am on any newly created post drafts? In both the Classic Editor and the Block Editor?
Edit: The Block Editor uses React; this creates some issues. https://wordpress.org/support/topic/how-can-i-set-the-time-on-a-new-post-to-8am-using-an-action-hook/
I write a week's worth of scheduled posts at a time, and I change the time to 8am on each so that they are published early and the "new post" emails go out early. It will save a few steps if each time I create a new post, the time is set to 8am. I don't want to set the date, only the time.
I suppose I need to hook into the new post function and use wp_insert_post_data
. Then I can then set the date in the future for a scheduled publication.
I know there are 4 date/time fields: post_date, post_modified, and their GMT variants. Date/time values are in this format: 2024-10-01 12:00:00
I'd also like to be sure that the function to change the time only runs once, on new post creation, so that the user can change the time, if needed, in a future post revision.
you can automate this hour selection with the following code. take into account that this code also affect the date of a post created trough the API.
add_filter("wp_insert_post_data", function ($data, $postarr, $unsanitized_postarr, $update) {
if ( !$update
&& ("post" === $data["post_type"])
&& ("auto-draft" === $data["post_status"])
) {
$date = current_datetime()
->modify("tomorrow 08:00:00")
->format("Y-m-d H:i:s")
;
$data["post_date"] = $date;
}
return $data;
}, 10, 4);
what is also possible to do is to retrieve the last draft that you saved and then set the next draft date to the next day instead of tomorrow.
Use a different hook instead, it will work on both create & update events:
add_action( 'save_post', 'set_time_to_eight_am', 999, 2 );
function set_time_to_eight_am( $post_id, $post ){
remove_action( 'save_post', 'set_time_to_eight_am', 999 );
$post->post_date = explode(' ', $post->post_date)[0] . ' 08:00:00';
wp_update_post( $post );
add_action( 'save_post', 'set_time_to_eight_am', 999, 2 );
}
sorry for the formatting, hard to write code in this wiki style textarea
UPDATE: You need to change data to the new published post, so you need to know the post_id before change some of its metadata. Using the filter 'wp_insert_post_data' could not work in some conditions, due to plugin conflicts or other environment behaviour.
The code below should work as expected. Please try it.
function set_default_post_time_to_8am_after_publish( $post_id, $post, $update ) {
// Check if the post is new (not updated) and is of type "draft" or "auto-draft"
if ( $update || $post->post_status !== 'draft' ) {
return;
}
// Check if the post has already been processed
if ( get_post_meta( $post_id, '_set_8am_time', true ) ) {
return;
}
// Set the post date to 8:00 AM
$current_date = substr( $post->post_date, 0, 10 ); // Get only the date (YYYY-MM-DD)
$new_post_date = $current_date . ' 08:00:00';
$new_post_date_gmt = get_gmt_from_date( $new_post_date ); // Adjust GMT time
// Update the values in the database
global $wpdb;
$wpdb->update(
$wpdb->posts,
array(
'post_date' => $new_post_date,
'post_date_gmt' => $new_post_date_gmt,
),
array( 'ID' => $post_id )
);
// Add a meta field to indicate that the post has been processed
update_post_meta( $post_id, '_set_8am_time', true );
}
add_action( 'wp_after_insert_post', 'set_default_post_time_to_8am_after_publish', 10, 3 );