WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this function:

<?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5'); ?>
    <?php while ($popular->have_posts()) : $popular->the_post(); ?> 
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li>
<?php endwhile; ?>

Where I added $popular->wp_comment_count, but it did not work at all. How do I do this? I want to let see the comment count per post...

share|improve this question
up vote 3 down vote accepted

Try this:

<?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5'); ?>
<?php while ($popular->have_posts()) : $popular->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> has <?php comments_number( 'no responses', 'one response', '% responses' ); ?>.</li>
<?php endwhile; ?>

From the codex.

share|improve this answer

There is no wp_comment_count in a WP_Query object, either in the "top" level as in your code-- $popular->wp_comment_count-- or in the individual post objects which is what you actually want to be checking.

There is a comment_count in the post objects though, and that is what you want-- $post->comment_count.

Your code should look like this (cleaned up and formatted into something readable):

while ($popular->have_posts()) : 
  $popular->the_post(); ?> 
  <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li><?php 
  echo $post->comment_count; // here is your count

Notice that you are checking $post for the comment count. $post is a global set for each item in the Loop by $popular->the_post(). If you are trying to display the count, as opposed to do some other operations based on the comment count, you should probably use comments_number as in the answer by @NathanPowell

share|improve this answer

Use my way

$commentscount = get_comments_number();
if($commentscount == 1): $commenttext = 'comment'; endif;
if($commentscount > 1 || $commentscount == 0): $commenttext = 'comments'; endif;
echo $commentscount.' '.$commenttext;

its working in all version.

share|improve this answer

Your Answer


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.