0

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 );
    }
}

1 Answer 1

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.org/browser/trunk/src/wp-includes/comment.php#L1748

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.