Create Page To Count Comments For Each Post In WordPress Site

This is pretty easy to do. Put this code in your functions.php file.

The logic is pretty straightforward.

  • Get all posts with get_posts().
  • Iterate over all post, extract comments count and permalink of current post and
  • Print out the results.

.

add_action( 'init', 'get_comments_count' ); // Hook to init, elsewhere or use directly in your code
function get_comments_count() {

  $all_posts = get_posts( array( 'numberposts' => -1 ) );

  foreach( $all_posts as $current_post ){

    $comments_count = get_comment_count( $current_post->ID );
    $permalink = get_permalink( $current_post->ID );

    printf( '<a href="https://wordpress.stackexchange.com/questions/233878/%s">%s</a> - %s<br>', $permalink, $permalink, $comments_count['total_comments'] );

  }
}