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.

  • Based on the previous answers to your questions you should already be able to write such a function.
    – fuxia
    May 14, 2013 at 17:53

3 Answers 3


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.


You can use it like this for the current logged in user:

global $current_user;
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
  • ok, please try the updated answer.
    – birgire
    May 16, 2013 at 10: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 function get_the_author_meta('ID') works for me in for example the TwentyTwelve theme after the the_post() call in author.php.
    – birgire
    May 17, 2013 at 8:19
  • 1
    I added an usage example when you are outside the loop also.
    – birgire
    May 17, 2013 at 8:27

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;
 $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 ) {
    } else {
 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.
    – Brian
    May 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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