samedi 31 janvier 2015

Get a List of All Commenters on the Current Post


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;
endforeach;

$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?





Aucun commentaire:

Enregistrer un commentaire