admin管理员组文章数量:1292977
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;
}
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;
}
Share
Improve this question
edited May 13, 2013 at 16:38
user1627363
asked May 13, 2013 at 14:48
user1627363user1627363
1724 silver badges21 bronze badges
2 Answers
Reset to default 3There 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.
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
本文标签: wp queryHow to output comments number of a post per day
版权声明:本文标题:wp query - How to output comments number of a post per day? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741567065a2385788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论