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.

2
  • No, there is no such nice function. You have to develop it by yourself. Commented Feb 7, 2013 at 17:46
  • @EugeneManuilov - that is what this site is here for! :-) Commented Feb 7, 2013 at 17:52

2 Answers 2

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.

3
  • 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. Commented Feb 8, 2013 at 10:33
  • 'post_id' = $post->ID,
    – Max Yudin
    Commented Feb 8, 2013 at 10:38
  • btw sorry AndrettiMilas for not crediting but I did already find this code and was the last bit I needed. Commented Feb 8, 2013 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

2
  • You can use 'count' => true argument to eliminate count() call. If set to true, get_comments() will return comments count only.
    – Max Yudin
    Commented Feb 7, 2013 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. Commented Feb 8, 2013 at 9: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.