简体   繁体   English

如何获取已批准评论的帖子ID?

[英]How to get the post id of approved comments?

How to get the post id of approved comments?如何获取已批准评论的帖子ID? 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.您定义的函数的第三个参数是一个WP_Comment对象,您需要访问该对象以获取帖子的 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. get_the_ID()仅在循环中工作,并检索帖子的 ID,而不是评论的 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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