I am trying to figure out if there is a way to change the status of a post to private after a comment has been submitted. More specifically 3 comments, I do already have some code made up and would like to add this function within it. Would it be possible for me to hook it in this code some how?

    global $post,$current_user;
$args = array( 'post_id' => $post->ID );
$comment = get_comments( $args );
  if ($post->post_author == $current_user->ID && 3 <= count( $comment ) ){
    echo do_shortcode( '[button]' );
} elseif ( 3 <= count( $comment ) ) {
} else {
  • Based on your question, it sounds like you want to make the post private after three comments but by the looks of your code it looks like you want to rather disable comments once there are more than three. I think either the comment_post action or the comments_open filter should get you going in the right direction, though. Commented Oct 18, 2016 at 21:09
  • That is what that code does but not what I'm trying to accomplish. I added that code, wondering if I could add what I want to it.
    – David
    Commented Oct 21, 2016 at 18:38
  • I have decided to go with making new conditions in functions.php 'global $post; $args = array( 'post_id' => $post->ID ); $comment = get_comments( $args ); if ( 3 <= count( $comment ) ){ $post = array( 'ID' => '1015', 'get_comments' => '3', 'post_status' => 'private' ); wp_update_post($post); } else { //blank }' that is the code I have tried adding to test it for post 1015. I does change the status to private but even if it has 0 comments. I would like for it to change the status to private after 3 comments. I would also like it to be for all posts, I used 1015 as a test.
    – David
    Commented Oct 21, 2016 at 18:40

1 Answer 1


You were pretty close. You just want to hook in at the right time when the comment is being saved. This is untested but should work.

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 ) {

    $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.
  • I have tried using your code but get the following error 'Fatal error: Cannot use object of type WP_Comment as array'
    – David
    Commented Nov 2, 2016 at 2:52
  • @David My mistake, I forgot the default output of get_comment() is an object unless you specifically request an array. I have edited my answer, please try again. Commented Nov 2, 2016 at 15:02
  • It's so close but not quite.
    – David
    Commented Nov 2, 2016 at 21:33
  • It's going to private before it hits 3 comments. I've tried changing a few things around so it doesn't do that but still can't seem to get it to work.
    – David
    Commented Nov 2, 2016 at 21:34
  • @David I just tried the code on a fresh installation and it worked (post went private just after 3rd comment was added). You may want to try on a fresh install to rule out any other plugins or code you have running. Commented Nov 3, 2016 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.