1

is there a nice function I can use to limit logged in users to one comment per post? I'm setting up a review system and only one review permitted per user. Thanks.

  • No, there is no such nice function. You have to develop it by yourself. – Eugene Manuilov Feb 7 '13 at 17:46
  • @EugeneManuilov - that is what this site is here for! :-) – AndrettiMilas Feb 7 '13 at 17:52
1
<?php
global $current_user;
$args = array(
    'user_id' => $current_user->ID,
    'post_id' = 100, // post ID. Can be '$post->ID' depending where you are calling this from
    'count' => true
    );
if(get_comments($args) >= 1){
    echo 'disabled';
} else {
    comment_form();
}

Function_Reference/get_comments

Based on AndrettiMilas code.

| improve this answer | |
  • How would I code the '$post->ID' bit? There are many posts so don't think I can use 'post_id' = 100 - I am calling this from comments.php thanks. – user1684099 Feb 8 '13 at 10:33
  • 'post_id' = $post->ID, – Max Yudin Feb 8 '13 at 10:38
  • btw sorry AndrettiMilas for not crediting but I did already find this code and was the last bit I needed. – user1684099 Feb 8 '13 at 10:49
2

See if this still works:

<?php
global $current_user;
$args = array('user_id' => $current_user->ID);
$usercomment = get_comments($args);
if(count($usercomment) >= 1){
    echo 'disabled';
} else {
    comment_form();
}
?>

My source

| improve this answer | |
  • You can use 'count' => true argument to eliminate count() call. If set to true, get_comments() will return comments count only. – Max Yudin Feb 7 '13 at 21:22
  • Thanks AndrettiMilas but this function works only looking at all posts - I need to modify it to only look at the current post? Thanks. – user1684099 Feb 8 '13 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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