Is this possible? I've never seen it used, but I need it... I want to show for each author (if they are logged in) on their own profile page, the amount of comments they have posted today. If the day passes by, than the counter has to be 0 and the comment counter has to start again.
3 Answers
You can try this query that counts user comments by using the user_id
field in the comments table as a filter:
function count_user_comments_today( $uid ){
global $wpdb;
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', time() + 86400);
$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->comments} WHERE user_id = %d AND comment_date >= %s AND comment_date < %s ", $uid, $today, $tomorrow ));
return $count;
}
where we use PHP to give the current date and we don't use any SQL date functions on each row.
You could also consider using the WP_Comment_Query class if you are looking for a more native solution.
Usage:
You can use it like this for the current logged in user:
global $current_user;
get_currentuserinfo();
echo count_user_comments_today( $current_user->ID );
and for the current author:
echo count_user_comments_today( get_the_author_meta('ID') );
in the loop in the template page author.php
.
Outside the loop in author.php
you can use:
global $wp_query;
$curauth = $wp_query->get_queried_object();
echo count_user_comments_today( $curauth->ID );
for the current author.
-
We are close... The problem with this is that it gets the LOGGED IN users comments. If I go to an author profile, I want for each author the comment amount per day. Now it shows the logged in users comment number, which is the same on all author pages. Can you solve that? May 16, 2013 at 7:31
-
-
Nope, did not work either. It shows a zero for an author, even if I post messages. May 17, 2013 at 7:32
-
I guess you are then outside the loop in
author.php
, please try it inside the loop. The functionget_the_author_meta('ID')
works for me in for example the TwentyTwelve theme after thethe_post()
call inauthor.php
.– birgireMay 17, 2013 at 8:19 -
1
Yes it's definitely possible. Refer to http://codex.wordpress.org/Function_Reference/get_comment, you'll need to get the current server time in UNIX format and then convert comment_date_gmt
from each returned be using a foreach
loop
function current_user_comments_today() {
global $comment;
global $current_user;
get_currentuserinfo();
$all_user_comments = get_comments( array( 'comment_author_email' => $current_user-> user_email) );
$today = strtotime('today GMT');
$tomorrow = strtotime('today +1 GMT');
$comment_count = 0;
foreach ( $all_user_comments as $key => $comment ) {
$comment_time = strtotime( $comment['comment_date_gmt'] );
if ( $comment_time > $tomorrow && $comment_time < $today ) {
$comment_count++;
} else {
continue;
}
}
return $comment_count;
}
-
Obviously this is going to be quite slow so any suggestions to limit the query scope are welcome, but I don't think
get_comments()
is very friendly to a scope limit by time... could be wrong though.– BrianMay 14, 2013 at 17:25
I don't know of a way to directly pull this query with Core functions, but the SQL is simple.
$sql = "SELECT COUNT(comment_ID)
FROM {$wpdb->comments}
WHERE comment_author = 'admin'
AND DATE(comment_date) = FROM_UNIXTIME('".time()."')";
$a = $wpdb->get_var($sql);
You can alternately use something like AND DATE(comment_date) = FROM_UNIXTIME('".time()."')";
to pick out any particular date.