WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp count comments

Description

Retrieve a total comment count for a site or post.

Usage

 <?php wp_count_commentspost_id ); ?> 

Parameters

$post_id
(integer) (optional) The post ID to retrieve a comment count for.
Default: None

Returns

Returns an object with values approved, moderated, spam, trash, total_comments.

Examples

Default Usage

Retrieve comment count for a site.

<?php
$comments_count = wp_count_comments();
echo "Comments for site <br />";
echo "Comments in moderation: " . $comments_count->moderated . "<br />"; 
echo "Comments approved: " . $comments_count->approved . "<br />";
echo "Comments in Spam: " . $comments_count->spam . "<br />";
echo "Comments in Trash: " . $comments_count->trash . "<br />";
echo "Total Comments: " . $comments_count->total_comments . "<br />";
?>

Retrieve Comment Count for a Post

<?php
$comments_count = wp_count_comments( 2492 );
echo "Comments for post 2492 <br />";
echo "Comments in moderation: " . $comments_count->moderated . "<br />"; 
echo "Comments approved: " . $comments_count->approved . "<br />";
echo "Comments in Spam: " . $comments_count->spam . "<br />";
echo "Comments in Trash: " . $comments_count->trash . "<br />";
echo "Total Comments: " . $comments_count->total_comments . "<br />";
?>

Changelog

Since 2.5.0

Source File

wp_count_comments() is located in wp-includes/comment.php.

Related

Count Tags: wp_count_posts(), wp_count_terms(), wp_count_comments(), count_users()

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.