wppaste
WordPress

get_comment( WP_Comment|string|int $comment = null, string $output = OBJECT ): WP_Comment|array|null

Since
2.0.0
Source
wp-includes/comment.php:221

Normalizes a comment ID, WP_Comment instance, or the global $comment into a single WP_Comment object or array. The $output parameter controls whether the result comes back as a WP_Comment object, an associative array, or a numeric array, and every value passes through the get_comment filter before it reaches your code. Passing an invalid ID, an empty value with no global $comment set, or a value the filter rejects returns null, so check the result before reading properties or array keys. For bulk lookups, get_comments() or WP_Comment_Query scale better since this function resolves one comment at a time.

Retrieves comment data given a comment ID or comment object.

Description

If an object is passed then the comment data will be cached and then returned after being passed through a filter. If the comment is empty, then the global comment variable will be used, if it is set.

Compatibility

WordPress
since 2.0.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

$commentWP_Comment|string|intoptional
Comment to retrieve.Default: null
$outputstringoptional
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Comment object, an associative array, or a numeric array, respectively. Default OBJECT.Default: OBJECT

Return value

WP_Comment|array|null
Depends on $output value.

Common problems and fixes · 4

Why does get_comment() return null instead of a comment?

The function returns null whenever $_comment can't be built at all (no numeric ID, no WP_Comment, and no global $comment set) or when the get_comment filter replaces the value with something that isn't a WP_Comment instance.

Why did I get an array back instead of a WP_Comment object?

The second parameter, $output, decides the return shape. Passing ARRAY_A or ARRAY_N converts the WP_Comment to an array via to_array(), so ->comment_content style property access will fail.

Is the data from get_comment() safe to print directly?

No. get_comment() only fetches and filters the raw comment row (or row-derived object); it does not escape comment_author, comment_content, or comment_author_url for output.

Why did I lose custom properties when I passed my own object?

When $comment is an object but not already a WP_Comment, the source does 'new WP_Comment( $comment )', which only copies the properties WP_Comment recognizes. Any extra properties on your object are dropped from the result.

Alternatives and related functions

get_comments
When you need a list of comments matching criteria such as post ID or status, rather than a single known comment.
WP_Comment_Query
When the lookup involves multiple filters, ordering, or pagination that a single get_comment() call can't express.
comment_exists
When you only need to check whether a comment by a given author and content already exists, for example to prevent duplicates.

Performance profile

How much work a call to get_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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
10–38

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

Plugin surface
1 hook

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

Called by
50

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

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 8 distinct outcomes

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

WhenInstructionsCalls it makes
always10–19none
!($comment instanceof) && !is_object($comment)14–18::WP_Comment()
always18–33apply_filters()
!($comment instanceof) && !is_object($comment)22–32::WP_Comment(), apply_filters()
always23–32apply_filters(), ->to_array()
!($comment instanceof) && !is_object($comment)27–31::WP_Comment(), apply_filters(), ->to_array()
always29–38apply_filters(), ->to_array(), array_values()
!($comment instanceof) && !is_object($comment)33–37::WP_Comment(), apply_filters(), ->to_array(), array_values()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5010–388
8.55010–388
8.45010–388
8.35010–388
8.25010–388
8.15010–3882 fewer instructions than PHP 7.4
7.45210–408

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 · 1

One hook fires while get_comment() runs, in this order:

  1. apply_filters( get_comment )filterline 245 (+24 into the body)

    Fires after a comment is retrieved.

Uses · 3

Used by · 50

Show all 50

Source code

function get_comment( $comment = null, $output = OBJECT ) {	if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {		$comment = $GLOBALS['comment'];	} 	if ( $comment instanceof WP_Comment ) {		$_comment = $comment;	} elseif ( is_object( $comment ) ) {		$_comment = new WP_Comment( $comment );	} else {		$_comment = WP_Comment::get_instance( $comment );	} 	if ( ! $_comment ) {		return null;	} 	/**	 * Fires after a comment is retrieved.	 *	 * @since 2.3.0	 *	 * @param WP_Comment $_comment Comment data.	 */	$_comment = apply_filters( 'get_comment', $_comment ); 	if ( OBJECT === $output ) {		return $_comment;	} elseif ( ARRAY_A === $output ) {		return $_comment->to_array();	} elseif ( ARRAY_N === $output ) {		return array_values( $_comment->to_array() );	}	return $_comment;}

Changelog

Introduced in 2.0.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 6.9.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.