0

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.

0

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();
}
  • And how can i use both? to check for author email and user id... – Christopher 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) – nonsensecreativity 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 Apr 3 '13 at 6:58
1

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.

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.