0

I have a question about the comment numbers in a post... For now, I can output the total number of a comment inside a post outside the loop. That's cool enough, but I want to get show the comment number/amount of a post PER DAY. So let's say I have a post and people are commenting 10 times today... Than, I want to show ' 10 comments' after the post title... If the day goes by and just 1 person comments the next day, I want it to output '1 comment' after the post title...

Is this possible, if yes, how?

I want to use the following function to output this "comment today per post after each post":

function wowPosts($num) {
    global $wpdb;

    $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");

    foreach ($posts as $post) {
        setup_postdata($post);
        $id = $post->ID;
        $title = $post->post_title;
        $count = $post->comment_count;
        $comment_count = get_comment_count($post->ID);
        $all_comments = get_comment_count( array ( 'post_id' => get_the_ID() ) );

        if ($count != 0) {
            $popular .= '<li>';
            $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> '. count( $all_comments ) . ' ';
            $popular .= '</li>';
        }
    }
    return $popular;
}

2 Answers 2

3

There is no argument to restrict a comment query directly to a given date. You have to filter the query later:

/**
 * Get the number of comments for a post today.
 *
 * @param  int $post_id
 * @return int
 */
function t5_count_comments_today( $post_id = NULL )
{
    if ( NULL === $post_id )
        $post_id = get_the_ID();

    add_filter( 'comments_clauses', 't5_comments_today_sql' );

    // get_comments() returns a string even with 'count' set to TRUE.
    return (int) get_comments(
        array (
            'post_id' => $post_id,
            'count'   => TRUE
        )
    );
}

/**
 * Add date specific WHERE clause to comment query.
 *
 * Deactivates itself to avoid collisions with regular comment queries.
 *
 * @wp-hook comments_clauses
 * @param   array $clauses
 * @return  array
 */
function t5_comments_today_sql( $clauses )
{
    // avoid collisions
    remove_filter( current_filter(), __FUNCTION__ );

    global $wpdb;
    $clauses['where'] .= $wpdb->prepare( ' AND comment_date >= %s', date( 'Y-m-d 00:00:00' ) );
    return $clauses;
}

Simple usage

echo t5_count_comments_today();

Or, if you don’t want to show zero comments:

$comments_today = t5_count_comments_today();
if ( 0 < $comments_today )
    echo $comments_today;

Extended use case

This adds the number to the text of comments_popup_link().

is_admin() || add_filter( 'comments_number', 't5_comment_number_today', 10, 2 );

/**
 * Add formatted number to total comments number.
 *
 * @wp-hook comments_number
 * @param   string $output
 * @param   int    $number Might be a string, so we cast it to int.
 * @return  string
 */
function t5_comment_number_today( $output, $number )
{
    // no comments at all, no need to count today's comments
    if ( 0 === (int) $number )
        return $output;

    $count = t5_count_comments_today();
    $add   = 'no comments today'; // default text

    if ( 1 === $count )
        $add = 'one comment today';

    if ( 1 < $count )
        $add = "$count comments today";

    return "$output ($add)";
}

Side note: Always use get_comments() or the class WP_Comment_Query for which get_comments() is a wrapper, because that result will be cached, so you will never have to fetch the same value for an identical argument list twice.
Avoid non-API calls with raw SQL in these cases.

9
  • It only shows a zero... I want to have a comment number per day per post (the posts in a list). Nothing happens here, beside the zero of course. Commented May 13, 2013 at 16:33
  • 1
    Comments per day or comments today? These are two completely different values. For the current day, this code works. It has to be called in the loop or with a post ID.
    – fuxia
    Commented May 13, 2013 at 16:36
  • "Show the comment number/amount of a post PER DAY", is what I said, "comments today per post" is what I meant... Commented May 13, 2013 at 16:37
  • Is it possible to do what I want? Because it looks a bit hard to me. Commented May 14, 2013 at 7:23
  • I don’t understand what doesn’t work in your setup. I tested this in mine, and it worked.
    – fuxia
    Commented May 14, 2013 at 9:22
1

The database table that WordPress uses to store its comments has a couple date fields in it -- comment_date and comment_date_gmt. You should be able to do something like this:

function wpse99287_comment_count( $content ) {
    global $wpdb;
    global $post;
    $today = date( 'Y-m-d 00:00:00' );
    $tomorrow = date( 'Y-m-d 23:59:59' );
    $sql = "SELECT COUNT(comment_id)
            FROM {$wpdb->comments} 
            WHERE comment_post_ID = {$post->ID}
            AND comment_approved = 1
            AND comment_date >= '$today'
            AND comment_date <= '$tomorrow'";
    $comment_count = $wpdb->get_var( $sql );
    $content = "<div class='comment-count'>$comment_count comment(s) today</div>" . $content;
}
add_filter( 'the_content', 'wpse99287_comment_count' );

That will add the comment count in a <div> at the start of your post's content.

References

wpdb class

2
  • That said, @toscho's answer looks like the better option, especially as the API calls allow caching of the results, which would lessen the load on your server.
    – Pat J
    Commented May 13, 2013 at 15:42
  • I agree, but here also nothing happens. I want to use the function I have edited in my first post... I want to output the comment number per day side to side with the post title. Commented May 13, 2013 at 16:36

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.