# Close Comments After a Set Number

> Closes the comment form on a post once it has reached the number of approved comments you choose, keeping threads manageable.

- Type: PHP
- Runs: Run everywhere
- Category: [Comments](https://wpide.com/library/category/comments/)
- Tags: comments, moderation, limits
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/close-comments-after-count/

## Code

```php
// Close comments once a post has this many approved comments.
$wpide_max_comments_per_post = 200;

add_filter( 'comments_open', function ( $open, $post_id ) use ( $wpide_max_comments_per_post ) {
    if ( ! $open || ! $post_id ) {
        return $open;
    }

    return (int) get_comments_number( $post_id ) < $wpide_max_comments_per_post;
}, 20, 2 );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Close Comments After a Set Number" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
