Automatically turn off the comment function of WordPress articles when the number of comments reaches a certain number

The comment function of WordPress articles will be automatically turned off after the limit number of days is exceeded. This is a good way to limit comments. When the number of comments reaches a certain number, the comment function of WordPress articles will be turned off automatically. This is also a good choice!

Add the following code to the theme’s functions.php file:

1
2
3
4
5
6
7
8
9
10
11
12
//Comments are closed 
if the number of comments exceeds a certain number function disable_comments (  $posts  )  { 
	if  (  ! Is_single ( )  )  {  
		return  $posts ;  
	} 
	if  (  $posts [ 0 ] -> comment_count  >  100  )  { 
		$posts [ 0 ] -> comment_status  =  'disabled' ; 
		$posts [ 0 ] -> ping_status  =  'disabled' ; 
	} 
	return $posts ; 
} 
add_filter (  'the_posts' ,  'disable_comments'  ) ;

The above code is to automatically turn off the comment function of this article when the number of comments on an article exceeds 100. Please modify the 100 in line 6 to the number you want according to your needs.

This code is valid in WP 3.5.1 test.

Reference materials: http://wordpressapi.com/2013/06/12/disable-comments-posts-commment-count-wordpress/

Leave a Reply

Your email address will not be published. Required fields are marked *