Update Modified date when scheduled posts get published

I have a post scheduled to go out 1 week from now. I made the change today to schedule it and let it be.When it finally

I have a post scheduled to go out 1 week from now. I made the change today to schedule it and let it be.

When it finally gets published, the post date is the date it was scheduled to be published. But the modified date is still 1 week earlier!

How do I make it so that as soon as a scheduled post gets published it updates the modified date to the post date?

I have a post scheduled to go out 1 week from now. I made the change today to schedule it and let it be.

When it finally gets published, the post date is the date it was scheduled to be published. But the modified date is still 1 week earlier!

How do I make it so that as soon as a scheduled post gets published it updates the modified date to the post date?

Share Improve this question asked Oct 26, 2017 at 11:10 Molten IceMolten Ice 1116 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

With the help of someone else, I managed to figure this out.

Add this to your functions.php:

// Scheduled posts should update modified date when published
function update_modified_date_to_post_date( $post ) {
    $updated_data = [
        'ID'                => $post->ID,
        'post_modified'     => $post->post_date,
        'post_modified_gmt' => $post->post_date_gmt
    ];
    wp_update_post( $updated_data );
}
add_action( 'future_to_publish', 'update_modified_date_to_post_date', 10, 1 );

wp_update_post actually sets the current time to post_modified and post_modified_gmt each time is called.

You can use a $wpdb query for this to actually work.

function update_modified_date_to_post_date( $post ) {
    global $wpdb;
    $wpdb->query("UPDATE $wpdb->posts SET post_modified = '{$post->post_date}', post_modified_gmt = '{$post->post_date_gmt}' WHERE ID = {$post->ID}" );
}
add_action( 'future_to_publish', 'update_modified_date_to_post_date', 10, 1 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745508175a4630647.html

相关推荐

  • Update Modified date when scheduled posts get published

    I have a post scheduled to go out 1 week from now. I made the change today to schedule it and let it be.When it finally

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信