[英]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.