wordpress自动将新帖子的发布时间设置为早上 8 点
wordpress自动将新帖子的发布时间设置为早上 8 点详细介绍
请改用不同的钩子,它将同时适用于创建和更新事件:
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 );