Search code examples
phpwordpressfilecommentsfopen

WP - How to get all the comment_post_ID in bulk comment approval


When I approve the comments in bulk, it only gets one comment_post_ID.
I need to get all the comment_post_ID.

This code:

add_action('transition_comment_status', 'my_approve_comment_callback', 10, 3);

function my_approve_comment_callback($new_status, $old_status, $comment) {

    if ($old_status != $new_status && $new_status == 'approved') {
        $my_file = fopen("/tmp/postidlist.txt", "w");

        $comment_id_2 = get_comment($comment->comment_ID);
        $comment_post_id = $comment_id_2->comment_post_ID;

        fwrite($my_file, $comment_post_id);
        fclose($my_file);
    }
}

Solution

  • Actually the problem lies in the fopen() mode; you are using w which stantds for write mode which overwrite the existing content, you have to use a mode which is used for append content to the existing file.

    Replace this line

    $my_file = fopen("/tmp/postidlist.txt", "w"); 
    

    with this

    $my_file = fopen("/tmp/postidlist.txt", "a"); 
    

    Hope this helps!