Title: get_comment_pages_count
Published: April 25, 2014
Last modified: February 24, 2026

---

# get_comment_pages_count( WP_Comment[] $comments = null, int $per_page = null, bool $threaded = null ): int

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#wp--skip-link--target)

Calculates the total number of comment pages.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#parameters)󠁿

 `$comments`[WP_Comment](https://developer.wordpress.org/reference/classes/wp_comment/)[]
optional

Array of [WP_Comment](https://developer.wordpress.org/reference/classes/wp_comment/)
objects. Defaults to `$wp_query->comments`.

Default:`null`

`$per_page`intoptional

Comments per page. Defaults to the value of `comments_per_page` query var, option
of the same name, or 1 (in that order).

Default:`null`

`$threaded`booloptional

Control over flat or threaded comments. Defaults to the value of `thread_comments`
option.

Default:`null`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#return)󠁿

 int Number of comment pages.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#source)󠁿

    ```php
    function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
    	global $wp_query;

    	if ( null === $comments && null === $per_page && null === $threaded && ! empty( $wp_query->max_num_comment_pages ) ) {
    		return $wp_query->max_num_comment_pages;
    	}

    	if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) {
    		$comments = $wp_query->comments;
    	}

    	if ( empty( $comments ) ) {
    		return 0;
    	}

    	if ( ! get_option( 'page_comments' ) ) {
    		return 1;
    	}

    	if ( ! isset( $per_page ) ) {
    		$per_page = (int) get_query_var( 'comments_per_page' );
    	}
    	if ( 0 === $per_page ) {
    		$per_page = (int) get_option( 'comments_per_page' );
    	}
    	if ( 0 === $per_page ) {
    		return 1;
    	}

    	if ( ! isset( $threaded ) ) {
    		$threaded = get_option( 'thread_comments' );
    	}

    	if ( $threaded ) {
    		$walker = new Walker_Comment();
    		$count  = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
    	} else {
    		$count = ceil( count( $comments ) / $per_page );
    	}

    	return (int) $count;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/comment.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/comment.php#L981)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/comment.php#L981-L1022)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#related)󠁿

| Uses | Description | 
| [get_query_var()](https://developer.wordpress.org/reference/functions/get_query_var/)`wp-includes/query.php` |

Retrieves the value of a query variable in the [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/) class.

  | 
| [Walker::get_number_of_root_elements()](https://developer.wordpress.org/reference/classes/walker/get_number_of_root_elements/)`wp-includes/class-wp-walker.php` |

Calculates the total number of root elements.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#)

| Used by | Description | 
| [get_the_comments_navigation()](https://developer.wordpress.org/reference/functions/get_the_comments_navigation/)`wp-includes/link-template.php` |

Retrieves navigation to next/previous set of comments, when applicable.

  | 
| [get_next_comments_link()](https://developer.wordpress.org/reference/functions/get_next_comments_link/)`wp-includes/link-template.php` |

Retrieves the link to the next comments page.

  | 
| [paginate_comments_links()](https://developer.wordpress.org/reference/functions/paginate_comments_links/)`wp-includes/link-template.php` |

Displays or retrieves pagination links for the comments on the current post.

  | 
| [wp_list_comments()](https://developer.wordpress.org/reference/functions/wp_list_comments/)`wp-includes/comment-template.php` |

Displays a list of comments.

  | 
| [comments_template()](https://developer.wordpress.org/reference/functions/comments_template/)`wp-includes/comment-template.php` |

Loads the comment template specified in $file.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#comment-content-1441)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_comment_pages_count/#comment-1441)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%23comment-1441)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%23comment-1441)
 4.  **Basic Usage**
 5.  This function can be used within the loop like this:
 6.      ```php
         $pages = get_comment_pages_count();
         ```
     
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%3Freplytocom%3D1441%23feedback-editor-1441)
 8.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#comment-content-1442)
 9.    [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_comment_pages_count/#comment-1442)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%23comment-1442)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%23comment-1442)
 11. **Number of comments per page**
 12. This will use the defaults for the number of comments per page and threading. 
     You can use custom values like this:
 13.     ```php
         // Show 25 comments per page.
         $pages = get_comment_pages_count( null, 25 );
     
         // Don't thread comments.
         $pages = get_comment_pages_count( null, null, false ); 
     
         // Show 10 comments per page, use threading.
         $pages = get_comment_pages_count( null, 10, true ); 
         ```
     
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%3Freplytocom%3D1442%23feedback-editor-1442)
 15.  [Skip to note 6 content](https://developer.wordpress.org/reference/functions/get_comment_pages_count/?output_format=md#comment-content-1443)
 16.   [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_comment_pages_count/#comment-1443)
 17. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%23comment-1443)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%23comment-1443)
 18. **Outside the loop**
 19. When inside the loop, you can just pass null as the value for the $comment parameter,
     as shown above. You can also use the function outside the loop, but you need to
     pass in the array of comments. For example, you may perform a custom comment query
     using `WP_Comment_Query`:
 20.     ```php
         $args = array(
            // query args here
         );
     
         $comments_query = new WP_Comment_Query;
         $comments = $comments_query->query( $args );
     
         $pages = get_comment_pages_count( $comments );
         ```
     
 21.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F%3Freplytocom%3D1443%23feedback-editor-1443)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_comment_pages_count%2F)
before being able to contribute a note or feedback.