get_comment_pages_count( WP_Comment[] $comments = null, int $per_page = null, bool $threaded = null ): int
- Since
- 2.7.0
- Source
wp-includes/comment.php:944
Compatibility
- WordPress
- since 2.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$commentsWP_Comment[]optional- Array of WP_Comment objects. Defaults to
$wp_query->comments.Default:null $per_pageintoptional- Comments per page. Defaults to the value of
comments_per_pagequery var, option of the same name, or 1 (in that order).Default:null $threadedbooloptional- Control over flat or threaded comments. Defaults to the value of
thread_commentsoption.Default:null
Return value
int- Number of comment pages.
Performance profile
How much work a call to get_comment_pages_count() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 12–61
- Plugin surface
- None
- Called by
- 6
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 71.
Nothing here hands control to plugin code.
6 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below get_comment_pages_count() - cacheobject cache
wp_cache_get()one call below get_comment_pages_count() - serializeserialisation
maybe_unserialize()one call below get_comment_pages_count()
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 17 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_comment_pages_count() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12–22 | none |
!empty($comments) | 16–32 | get_option() |
!empty($comments) && get_option() && isset($per_page) && $per_page === 0 | 27–37 | get_option(), get_option() |
!empty($comments) && get_option() && !isset($per_page) | 27–37 | get_option(), get_query_var() |
!empty($comments) && get_option() && isset($per_page) && $per_page !== 0 && isset($threaded) | 31–41 | get_option(), ceil() |
!empty($comments) && get_option() && !isset($per_page) && $per_page === 0 | 32–42 | get_option(), get_query_var(), get_option() |
!empty($comments) && get_option() && isset($per_page) && $per_page !== 0 | 35–46 | get_option(), get_option(), ceil() |
!empty($comments) && get_option() && !isset($per_page) && $per_page !== 0 && isset($threaded) | 36–46 | get_option(), get_query_var(), ceil() |
!empty($comments) && get_option() && isset($per_page) && $per_page !== 0 && isset($threaded) | 37–47 | get_option(), ->get_number_of_root_elements(), ceil() |
!empty($comments) && get_option() && isset($per_page) && !isset($threaded) | 40–50 | get_option(), get_option(), get_option(), ceil() |
!empty($comments) && get_option() && !isset($per_page) && $per_page !== 0 | 40–51 | get_option(), get_query_var(), get_option(), ceil() |
!empty($comments) && get_option() && isset($per_page) && $per_page !== 0 | 41–52 | get_option(), get_option(), ->get_number_of_root_elements(), ceil() |
5 further outcomes, up to 61 instructions
!empty($comments) && get_option() && !isset($per_page) && $per_page !== 0 && isset($threaded) | 42–52 | get_option(), get_query_var(), ->get_number_of_root_elements(), ceil() |
!empty($comments) && get_option() && !isset($per_page) && !isset($threaded) | 45–55 | get_option(), get_query_var(), get_option(), get_option(), ceil() |
!empty($comments) && get_option() && isset($per_page) && !isset($threaded) | 46–56 | get_option(), get_option(), get_option(), ->get_number_of_root_elements(), ceil() |
!empty($comments) && get_option() && !isset($per_page) && $per_page !== 0 | 46–57 | get_option(), get_query_var(), get_option(), ->get_number_of_root_elements(), ceil() |
!empty($comments) && get_option() && !isset($per_page) && !isset($threaded) | 51–61 | get_option(), get_query_var(), get_option(), get_option(), ->get_number_of_root_elements(), ceil() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 71 instructions, 12–61 executed per call, 14 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- get_option()Retrieves an option value based on an option name.
- get_query_var()Retrieves the value of a query variable in the WP_Query class.
- Walker_Comment::__construct()
Used by · 6
- comments_template()Loads the comment template specified in $file.
- get_next_comments_link()Retrieves the link to the next comments page.
- get_the_comments_navigation()Retrieves navigation to next/previous set of comments, when applicable.
- paginate_comments_links()Displays or retrieves pagination links for the comments on the current post.
- twentyfifteen_comment_nav()Display navigation to next/previous comments when applicable.
- wp_list_comments()Displays a list of comments.
Source code
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;}Changelog
Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.