你非常接近。您只想在保存评论的正确时间加入。这是未经测试的,但应该有效。
add_action( \'comment_post\', \'wpse_make_private_after_3_comments\', 10, 2 );
function wpse_make_private_after_3_comments( $comment_ID, $comment_approved ) {
$comment = get_comment( $comment_ID );
$post_ID = $comment->comment_post_ID;
$comments = wp_count_comments( $post_ID );
// You could also access approved, moderated, spam or trashed comments
// from the return object of wp_count_comments().
$comment_count = $comments->total_comments;
// If we only have 1 or 2 comments, we\'ll bail early
if ( $comment_count < 3 ) {
return;
}
$post_data = array(
\'ID\' => $post_ID,
\'post_status\' => \'private\'
);
wp_update_post( $post_data );
// You might want to add a wp_redirect() here to
// so people don\'t automatically see a 404 page
// when the comment saving is complete since the page will be private.
}