How do I refresh "Post Last Modified Time", as long as comment is updated to Approve status?

I read from another question and tried their code which is pasted below- Update post date on every new comment?This co

I read from another question and tried their code which is pasted below - Update post date on every new comment?

This code is working well, however it updates the time when people Insert Comment in the front-end. What I seek is to update the time when admin Approve Comment in the back-end.

I read in another question and tried to replace with the hook suggested, but with no success - Approve comment hook?

add_action('wp_insert_comment','update_post_time',99,2);
function update_post_time($comment_id, $comment_object) {
    // Get the post's ID
    $post_id = $comment_object->comment_post_ID;
    // Double check for post's ID, since this value is mandatory in wp_update_post()
    if ($post_id) {
        // Get the current time
        $time = current_time('mysql');
        // Form an array of data to be updated
        $post_data = array(
            'ID'           => $post_id, 
            'post_modified'   => $time, 
            'post_modified_gmt' =>  get_gmt_from_date( $time )
        );
        // Update the post
        wp_update_post( $post_data );
    }
}

I read from another question and tried their code which is pasted below - Update post date on every new comment?

This code is working well, however it updates the time when people Insert Comment in the front-end. What I seek is to update the time when admin Approve Comment in the back-end.

I read in another question and tried to replace with the hook suggested, but with no success - Approve comment hook?

add_action('wp_insert_comment','update_post_time',99,2);
function update_post_time($comment_id, $comment_object) {
    // Get the post's ID
    $post_id = $comment_object->comment_post_ID;
    // Double check for post's ID, since this value is mandatory in wp_update_post()
    if ($post_id) {
        // Get the current time
        $time = current_time('mysql');
        // Form an array of data to be updated
        $post_data = array(
            'ID'           => $post_id, 
            'post_modified'   => $time, 
            'post_modified_gmt' =>  get_gmt_from_date( $time )
        );
        // Update the post
        wp_update_post( $post_data );
    }
}
Share Improve this question asked Mar 17, 2020 at 14:37 user184418user184418 1
Add a comment  | 

1 Answer 1

Reset to default 0

It's easy, you can just use different hook to perform same action.

add_action( 'comment_approved_', 'update_post_time', 99, 2); // this will fire when comment is approved regardless of comment type 

Last underscore in action name is important comment_approved_ as normally after it goes comment-type if you need to hook to approval to specific comment types.

See https://core.trac.wordpress/browser/trunk/src/wp-includes/comment.php#L1748

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信