wppaste
WordPress

get_page_of_comment( int $comment_id, array $args = array() ): int|null

Since
2.7.0
Source
wp-includes/comment.php:1107
Calculates what page number a comment will appear on for comment paging.

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

$comment_idint
Comment ID.
$argsarrayoptional
Array of optional arguments.Default: array()
  • $typestringdefault: 'all'

    Limit paginated comments to those matching a given type. Accepts 'comment', 'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
  • $per_pageint

    Per-page count to use when calculating pagination. Defaults to the value of the 'comments_per_page' option.
  • $max_depthint|string

    If greater than 1, comment page will be determined for the top-level parent $comment_id. Defaults to the value of the 'thread_comments_depth' option.

Return value

int|null
Comment page number or null on error.

Performance profile

How much work a call to get_page_of_comment() 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
Heavy

Reaches the database via ->query().

Scaling
Constant

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

Instructions
10–124

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

Plugin surface
2 hooks

Third-party callbacks on 'get_page_of_comment_query_args', 'get_page_of_comment' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 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()called directly
  • sqldatabase query->query()called directly
  • cacheobject cachewp_cache_get()one call below get_page_of_comment()
  • serializeserialisationmaybe_unserialize()one call below get_page_of_comment()

Further down the call graph this can also reach transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 45 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_page_of_comment() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10get_comment()
$page !== null36–47get_comment(), wp_parse_args(), get_option(), apply_filters()
$page === null43–54get_comment(), wp_parse_args(), get_option(), get_page_of_comment()
get_option() && $page !== null47–52get_comment(), wp_parse_args(), get_option(), get_option(), apply_filters()
get_option() && $page !== null47–52get_comment(), wp_parse_args(), get_option(), get_query_var(), apply_filters()
$page === null49–60get_comment(), wp_parse_args(), get_option(), get_option(), get_page_of_comment()
get_option() && $page !== null52–57get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), apply_filters()
$page === null && get_option()53–65get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), get_page_of_comment()
get_option() && $page === null54–59get_comment(), wp_parse_args(), get_option(), get_query_var(), get_page_of_comment()
get_option() && $page === null59–65get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_page_of_comment()
get_option() && $page === null64–69get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), get_option(), get_page_of_comment()
get_option() && $page === null64–70get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), get_page_of_comment()
33 further outcomes, up to 123 instructions
get_option() && $page === null69–74get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), get_option(), get_page_of_comment()
$page === null && !is_user_logged_in() && $older_comment_count === 082–99get_comment(), wp_parse_args(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), apply_filters()
$page === null && is_user_logged_in() && $older_comment_count === 084–98get_comment(), wp_parse_args(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), apply_filters()
$page === null && !is_user_logged_in() && $older_comment_count !== 087–104get_comment(), wp_parse_args(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), ceil(), apply_filters()
$page === null && !is_user_logged_in() && $older_comment_count === 088–105get_comment(), wp_parse_args(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), apply_filters()
$page === null && is_user_logged_in() && $older_comment_count !== 089–103get_comment(), wp_parse_args(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), ceil(), apply_filters()
$page === null && is_user_logged_in() && $older_comment_count === 090–104get_comment(), wp_parse_args(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), apply_filters()
$page === null && get_option() && !is_user_logged_in() && $older_comment_count === 092–110get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count === 093–104get_comment(), wp_parse_args(), get_option(), get_query_var(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), apply_filters()
$page === null && !is_user_logged_in() && $older_comment_count !== 093–110get_comment(), wp_parse_args(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), ceil(), apply_filters()
$page === null && get_option() && is_user_logged_in() && $older_comment_count === 094–109get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count === 095–103get_comment(), wp_parse_args(), get_option(), get_query_var(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), apply_filters()
$page === null && is_user_logged_in() && $older_comment_count !== 095–109get_comment(), wp_parse_args(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), ceil(), apply_filters()
$page === null && get_option() && !is_user_logged_in() && $older_comment_count !== 097–115get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count !== 098–109get_comment(), wp_parse_args(), get_option(), get_query_var(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count === 098–110get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), apply_filters()
$page === null && get_option() && is_user_logged_in() && $older_comment_count !== 099–114get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count !== 0100–108get_comment(), wp_parse_args(), get_option(), get_query_var(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count === 0100–109get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count === 0103–114get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count !== 0103–115get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count === 0103–115get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count === 0105–113get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count !== 0105–114get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count === 0105–114get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count !== 0108–119get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count !== 0108–120get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count === 0108–119get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count !== 0110–118get_comment(), wp_parse_args(), get_option(), get_option(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count !== 0110–119get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count === 0110–118get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), apply_filters()
get_option() && $page === null && !is_user_logged_in() && $older_comment_count !== 0113–124get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), get_option(), is_user_logged_in(), wp_get_unapproved_comment_author_email(), apply_filters(), ->query(), ceil(), apply_filters()
get_option() && $page === null && is_user_logged_in() && $older_comment_count !== 0115–123get_comment(), wp_parse_args(), get_option(), get_query_var(), get_option(), get_option(), get_option(), is_user_logged_in(), get_current_user_id(), apply_filters(), ->query(), ceil(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 142 instructions, 10–124 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.

Hooks and filters fired · 2

2 hooks fire while get_page_of_comment() runs, in this order:

  1. apply_filters( get_page_of_comment_query_args )filterline 1210 (+103 into the body)

    Filters the arguments used to query comments in get_page_of_comment().

  2. apply_filters( get_page_of_comment )filterline 1252 (+145 into the body)

    Filters the calculated page on which a comment appears.

Uses · 10

Used by · 2

Source code

function get_page_of_comment( $comment_id, $args = array() ) {	global $wpdb; 	$page = null; 	$comment = get_comment( $comment_id );	if ( ! $comment ) {		return null;	} 	$defaults      = array(		'type'      => 'all',		'page'      => '',		'per_page'  => '',		'max_depth' => '',	);	$args          = wp_parse_args( $args, $defaults );	$original_args = $args; 	// Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option.	if ( get_option( 'page_comments' ) ) {		if ( '' === $args['per_page'] ) {			$args['per_page'] = get_query_var( 'comments_per_page' );		} 		if ( '' === $args['per_page'] ) {			$args['per_page'] = get_option( 'comments_per_page' );		}	} 	if ( empty( $args['per_page'] ) ) {		$args['per_page'] = 0;		$args['page']     = 0;	} 	if ( $args['per_page'] < 1 ) {		$page = 1;	} 	if ( null === $page ) {		if ( '' === $args['max_depth'] ) {			if ( get_option( 'thread_comments' ) ) {				$args['max_depth'] = get_option( 'thread_comments_depth' );			} else {				$args['max_depth'] = -1;			}		} 		// Find this comment's top-level parent if threading is enabled.		if ( $args['max_depth'] > 1 && '0' !== $comment->comment_parent ) {			return get_page_of_comment( $comment->comment_parent, $args );		} 		$comment_args = array(			'type'       => $args['type'],			'post_id'    => $comment->comment_post_ID,			'fields'     => 'ids',			'count'      => true,			'status'     => 'approve',			'orderby'    => 'none',			'parent'     => 0,			'date_query' => array(				array(					'column' => "$wpdb->comments.comment_date_gmt",					'before' => $comment->comment_date_gmt,				),			),		); 		if ( is_user_logged_in() ) {			$comment_args['include_unapproved'] = array( get_current_user_id() );		} else {			$unapproved_email = wp_get_unapproved_comment_author_email(); 			if ( $unapproved_email ) {				$comment_args['include_unapproved'] = array( $unapproved_email );			}		} 		/**

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.1.0 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.