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.

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.

share|improve this question
    
Based on the previous answers to your questions you should already be able to write such a function. –  toscho May 14 '13 at 17:53

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
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? –  user1627363 May 16 '13 at 7:31
    
ok, please try the updated answer. –  birgire May 16 '13 at 10:31
    
Nope, did not work either. It shows a zero for an author, even if I post messages. –  user1627363 May 17 '13 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 '13 at 8:19
1  
I added an usage example when you are outside the loop also. –  birgire May 17 '13 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;
 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; 
}
share|improve this answer
    
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 '13 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.

share|improve this answer

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.