[英]How to get the post id of approved comments?
如何获取已批准评论的帖子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);
}
}
}
您定义的函数的第三个参数是一个WP_Comment对象,您需要访问该对象以获取帖子的 id。
例如:
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()
仅在循环中工作,并检索帖子的 ID,而不是评论的 ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.