简体   繁体   中英

How to get the post id of approved comments?

How to get the post id of approved comments? I could not get it with the following 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) {
    if($new_status == 'approved') {

       $myfile = fopen("/tmp/postidlist.txt", "w");  
       fwrite($myfile, get_the_ID());

       fclose($myfile);
    }
  }
}

The third parameter of the function you are defining is a WP_Comment object, you need to access that object to get the post's id.

Eg:

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");  
       fwrite($my_file, $comment->comment_post_ID);

       fclose($my_file);
    }
  }
}

get_the_ID() works only in the loop , and retrieves the post's ID, no the comment's ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM