Limit Comments per Post
-
Is there another way to limit the comments?
In
Settings > Discussionyou can automatically close comments after a certain number of days. It’s not a comment-count limit, obviously, but it is an alternative.Thanks for your reply Bill.
I do know about that alternative though it does not interest me.
I have came up with a solution.
If you implement this script:global $post; $args = array( 'post_id' => $post->ID ); $comment = get_comments( $args ); if ( 3 <= count( $comment ) ) { echo ''; } else { comment_form(); }in comments.php file then no more comments will be allowed after 3 submissions.
I will mark this topic as resolved.Glad you got something figured out for this. I thought you would have known about the day-limit but just wanted to put it out there in case. Thanks for posting your solution, though I’d recommend turning this into a plugin. The next time
comments.phpgets an update, your changes will be lost.Thank you very much @eversatile for your snippet, it works really nicely and it has solved most of my current issues.
I’m bumping into this thread to ask you whether you could advise this wp noob and php desperate to limit the comment threshold set by your snippet to certain custom post type(s) only.
I tried to edit your array adding ‘myCPT’=> $post->post_type,
but it just did not work.
I’ realy appreciate if you could point me in the right direction.
Thanks in advance for your helpif( get_post_type() == 'post_type_slug' ) { $args = array( 'post_id' => $post->ID ); $comment = get_comments( $args ); if ( 3 <= count( $comment ) ) { echo ''; } else { comment_form(); }Thanks for you inuput @prangesco.
See if the following code will work for you. I am just starting my day and have not tried it out yet, but I think that should work.@eversatile
THanks for your super quick reply and for the code, really appreciated that.Just one thing to point out: assuming that the bolded { in the first line:
if( get_post_type() == ‘post_type_slug’ ) { was assumed to be a semicolon ; instead (and please correct me if I am wrong),
the code does not sort the desired effect: the comment threshold is still effective but, unfortunately it is still effective for all post types.I have been playing around with this for a little bit and have figured it out.
It has been a while since I wrote these lines and had to remind myself of why I did it that way.
Most custom post types that I make have their own seaerate comment.php file so I was unable to use it globally. Though, if you make one small change to the code and add it to the other comment.php file for your custom post type (assuming you have one), it should work.global $post; $args = array( 'psp_projects' => $post->ID ); $comment = get_comments( $args ); if ( 2 <= count( $comment ) ) { echo 'Comments Reached'; } else { comment_form(); }Changing
'post_id'to'projects' //(your post type)and placing the code in comments.php file for my projects then the code will work the same way for that post type.Hullo @eversatile,
I would like to thank you publicly for your help, I simply couldn’t have made it without your support. I actually used the second of three codes you provided, the last one resulting in comments being just shut off.I’m summing up here what I did for any future visitor to get advantage of eVersatile’s tips. Again all merit goes to eVersatile.
I needed to close comments on one of my many custom post types after a certain number of comments, and the snippet originally provided in this thread did it nicely, except that it applied to all post types. So:
a) I created atemplate for comments.php in my child theme: for all noobs like myself it means copying the comment.php file from Wp directory inside your child theme and renaming it, in my case to comments-myCPT.php;
b)I modified the newly created comments-myCPT as follows:
if( get_post_type() == ‘myCPTslug’ );
$args = array( ‘post_id’ => $post->ID );
$comment = get_comments( $args );
if ( 1 <= count( $comment ) ) {
echo ‘whatever you want to appear when comments are closed’;
} else {
comment_form();c)in the single-myCPT file (which I had previously created) I recalled the preferred comments template by adding the appropriate address to this line:
comments_template( ”, true );which in my case became
comments_template( ‘/comments-myCPT.php’, true );
and that’s all folks.
The topic ‘Limit Comments per Post’ is closed to new replies.
(@eversatile)
11 years ago
Good Evening,
Is it possible to limit the number of comments each post is allowed to receive?
I have tried this plugin https://wordpress.org/plugins/comments-limit/ though it has not been updated in quite some time. The plugin does work for one site but not a multisite setup.
Is there another way to limit the comments?
Thank you.