wppaste
WordPress

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:984
Calculates the total number of comment pages.

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_page query 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_comments option.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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–61

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 71.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
6

6 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()one call below get_comment_pages_count()
  • cacheobject cachewp_cache_get()one call below get_comment_pages_count()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
always12–22none
!empty($comments)16–32get_option()
!empty($comments) && get_option() && isset($per_page) && $per_page === 027–37get_option(), get_option()
!empty($comments) && get_option() && !isset($per_page)27–37get_option(), get_query_var()
!empty($comments) && get_option() && isset($per_page) && $per_page !== 0 && isset($threaded)31–41get_option(), ceil()
!empty($comments) && get_option() && !isset($per_page) && $per_page === 032–42get_option(), get_query_var(), get_option()
!empty($comments) && get_option() && isset($per_page) && $per_page !== 035–46get_option(), get_option(), ceil()
!empty($comments) && get_option() && !isset($per_page) && $per_page !== 0 && isset($threaded)36–46get_option(), get_query_var(), ceil()
!empty($comments) && get_option() && isset($per_page) && $per_page !== 0 && isset($threaded)37–47get_option(), ->get_number_of_root_elements(), ceil()
!empty($comments) && get_option() && isset($per_page) && !isset($threaded)40–50get_option(), get_option(), get_option(), ceil()
!empty($comments) && get_option() && !isset($per_page) && $per_page !== 040–51get_option(), get_query_var(), get_option(), ceil()
!empty($comments) && get_option() && isset($per_page) && $per_page !== 041–52get_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–52get_option(), get_query_var(), ->get_number_of_root_elements(), ceil()
!empty($comments) && get_option() && !isset($per_page) && !isset($threaded)45–55get_option(), get_query_var(), get_option(), get_option(), ceil()
!empty($comments) && get_option() && isset($per_page) && !isset($threaded)46–56get_option(), get_option(), get_option(), ->get_number_of_root_elements(), ceil()
!empty($comments) && get_option() && !isset($per_page) && $per_page !== 046–57get_option(), get_query_var(), get_option(), ->get_number_of_root_elements(), ceil()
!empty($comments) && get_option() && !isset($per_page) && !isset($threaded)51–61get_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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.