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

时间:2017-03-17 16:12:22

标签: php wordpress

如何获取已批准评论的帖子ID? 我无法通过以下代码获得它。

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

1 个答案:

答案 0 :(得分:0)

您定义的函数的第三个参数是WP_Comment对象,您需要访问该对象才能获得帖子的ID。

E.g:

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()仅适用于the loop,并检索帖子的ID,而不是评论的ID。

相关问题