I'm trying to get a list of everyone who's commented on a post, and then check that list against the current logged in user. If the current logged in user is included in that list, then I don't show the comment form, otherwise they can leave a comment. So essentially, I only want people to be able to comment once per post.

I think I'm close, but I keep pulling in a list of everyone who's commented on any post.

Here's my code:

    <?php global $post;
      $comments = get_comments($post->ID); // get all comment author emails
      foreach($comments as $comment) :
        $existingcommenters = $existingcommenters.', '.$comment->comment_author_email;

      $current_user = wp_get_current_user(); // then we check those emails against the current users email
      $commenting = $current_user->user_email; ?>

<?php $args = array( // i then list my arguments, which I've omitted for clarity here
    ); ?>

<?php if (strpos($existingcommenters,$commenting) !== false) { // if the user already has a review, don't let them post another ?>
    <p>You've already reviewed this business.</p>
<?php } else { comment_form($args); } ?>

So I thought $existingcommenters should only contain a list of people who've commented on this post, as $comments = get_comments($post->ID); should be setting that call to only get a list of comments for this post, right?

If I echo $post->ID anywhere in this code, it does return the current post's ID successfully.

But if I echo $existingcommenters, it shows a list of every single commenter.

Any idea what I might be doing wrong?

  • Can you please show us the completed code and tell us where it goes/how you got it working? Thanks
    – Pete
    Commented Feb 17, 2018 at 22:46
  • @Pete see my comment in the correct answer.
    – Nathan
    Commented Feb 19, 2018 at 14:18

1 Answer 1


get_comments do not get a post ID as parameter but rather an array in which you can set all kinds of query criteira including post ID. See the codex http://codex.wordpress.org/Function_Reference/get_comments.

You can even use the author_email parameter to make get_comments do most of the work for you.

  • Thanks @mark-kaplun, that lead me to the correct answer, which is changing my second line of code to this: $comments = get_comments( array ( 'post_id' => get_the_ID() ) );
    – Nathan
    Commented Feb 2, 2015 at 15:48

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.