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 Answers
<?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.
-
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
-
-
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
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();
}
?>
-
You can use
'count' => true
argument to eliminatecount()
call. If set totrue
,get_comments()
will return comments count only. 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