Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i have this code to limit the comments per user.

<!-- #only one comment -->
<?php global $current_user,$post;
$args = array('user_id' => $current_user->ID,'post_id' => $post->ID);
$usercomment = get_comments($args);
if(count($usercomment) >= 1){
    echo 'Thank you for your comment';
} else {
    comment_form();
} ?>

My question is: how can edit this code to check for the author email rather than user id

Thank you.

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

Try this, the code below will generate every comment by the $current_user->user_email for the author_email, if the $usercomment return something, then there is a comment by the current user so echo "thank you", but if it's not return anything output the form.

global $current_user,$post;

$usercomment = get_comments(array('author_email' => $current_user->user_email, 'post_id' => $post->ID));

if($usercomment) {
    echo "Thank You";
}
else {
    comment_form();
}
share|improve this answer
    
And how can i use both? to check for author email and user id... –  Christopher Vasilis Apr 2 '13 at 14:50
    
in the get_comments( $args ), you can view which argument is available in here codex.wordpress.org/Function_Reference/get_comments so if you want to check both author_email and user_id you can parse an array( 'author_email' => $current_user->user_email, 'user_id' => $current_user->ID) –  nackle Apr 2 '13 at 16:12
    
i did this but it didn't work: array('author_email' => $current_user->user_email,'user_id' => $current_user->ID,'post_id' => $post->ID) –  Christopher Vasilis Apr 3 '13 at 6:58
add comment

You shouldn't have to make a second query to get comments. You should be able to check the existing $comments object for the user email, and switch accordingly.

global $current_user;
get_currentuserinfo();
$showform = true;
// var_dump($comments);
// var_dump($current_user);
if (!empty($comments) && isset($current_user)) {
  foreach ($comments as $c) {
    if ($c->comment_author_email === $current_user->data->user_email) {
      $showform = false;
      break;
    }
  }
}
if(!$showform){
    echo 'Thank you for your comment';
} else {
    comment_form();
}

Of course, this only works for logged-in users, but I don't know how else you could organize it. If the user is not logged-in the software doesn't know what the user_email will be.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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