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.
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?
- Why did I get an array back instead of a WP_Comment object?
- Is the data from get_comment() safe to print directly?
- Why did I lose custom properties when I passed my own object?
Why does get_comment() return null instead of a comment?
Why did I get an array back instead of a WP_Comment object?
Is the data from get_comment() safe to print directly?
Why did I lose custom properties when I passed my own object?
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
- Scaling
- Constant
- Instructions
- 10–38
- Plugin surface
- 1 hook
- Called by
- 50
Touches nothing outside its own arguments.
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 50.
Third-party callbacks on 'get_comment' run inside this call, and their cost is not bounded by anything here.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–19 | none |
!($comment instanceof) && !is_object($comment) | 14–18 | ::WP_Comment() |
| always | 18–33 | apply_filters() |
!($comment instanceof) && !is_object($comment) | 22–32 | ::WP_Comment(), apply_filters() |
| always | 23–32 | apply_filters(), ->to_array() |
!($comment instanceof) && !is_object($comment) | 27–31 | ::WP_Comment(), apply_filters(), ->to_array() |
| always | 29–38 | apply_filters(), ->to_array(), array_values() |
!($comment instanceof) && !is_object($comment) | 33–37 | ::WP_Comment(), apply_filters(), ->to_array(), array_values() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 50 | 10–38 | 8 | |
| 8.5 | 50 | 10–38 | 8 | |
| 8.4 | 50 | 10–38 | 8 | |
| 8.3 | 50 | 10–38 | 8 | |
| 8.2 | 50 | 10–38 | 8 | |
| 8.1 | 50 | 10–38 | 8 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 52 | 10–40 | 8 |
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:
Uses · 3
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Comment::__construct()Constructor.
- WP_Comment::get_instance()Retrieves a WP_Comment instance.
Used by · 50
- WP_Comment_Query::fill_descendants()Fetch descendants for located comments.
- WP_Comment_Query::get_comments()Get a list of comments matching the query vars.
- WP_Comments_List_Table::column_comment()
- WP_Comments_List_Table::floated_admin_avatar()Adds avatars to comment author names.
- WP_REST_Comments_Controller::create_item()Creates a comment.
- WP_REST_Comments_Controller::delete_item()Deletes a comment.
- WP_REST_Comments_Controller::get_comment()Get the comment, if the ID is valid.
- WP_REST_Comments_Controller::update_item()Updates a comment.
- _get_comment_reply_id()Gets the comment's reply to ID from the $_GET['replytocom'].
- _wp_ajax_delete_comment_response()Sends back current comment total and new page links if they need to be updated.
- comment_author()Displays the author of the current comment.
- comment_author_email()Displays the email of the author of the current global $comment.
Show all 50
- comment_author_url()Displays the URL of the author of the current comment, not linked.
- comment_excerpt()Displays the excerpt of the current comment.
- comment_form_title()Displays text based on comment reply status.
- comment_text()Displays the text of the current comment.
- edit_comment_link()Displays the edit comment link with formatting.
- floated_admin_avatar()Adds avatars to relevant places in admin.
- get_avatar()Retrieves the avatar `` tag for a user, email address, MD5 hash, comment, or post.
- get_avatar_data()Retrieves default data about the avatar.
- get_comment_ID()Retrieves the comment ID of the current comment.
- get_comment_author()Retrieves the author of the current comment.
- get_comment_author_IP()Retrieves the IP address of the author of the current comment.
- get_comment_author_email()Retrieves the email of the author of the current comment.
- get_comment_author_email_link()Returns the HTML email link to the author of the current comment.
- get_comment_author_link()Retrieves the HTML link to the URL of the author of the current comment.
- get_comment_author_url()Retrieves the URL of the author of the current comment, not linked.
- get_comment_class()Returns the classes for the comment div as an array.
- get_comment_date()Retrieves the comment date of the current comment.
- get_comment_excerpt()Retrieves the excerpt of the given comment.
- get_comment_guid()Retrieves the feed GUID for the current comment.
- get_comment_link()Retrieves the link to a given comment.
- get_comment_reply_link()Retrieves HTML content for reply to comment link.
- get_comment_text()Retrieves the text of the current comment.
- get_comment_time()Retrieves the comment time of the current comment.
- get_comment_to_edit()Returns a WP_Comment object based on comment ID.
- get_comment_type()Retrieves the comment type of the current comment.
- get_commentdata()Retrieve an array of comment data about comment $comment_id.
- get_edit_comment_link()Retrieves the edit comment link.
- get_object_subtype()Returns the object subtype for a given object ID of a specific type.
- get_page_of_comment()Calculates what page number a comment will appear on for comment paging.
- map_meta_cap()Maps a capability to the primitive capabilities required of the given user to satisfy the capability being checked.
- render_block_core_avatar()Renders the `core/avatar` block on the server.
- render_block_core_comment_author_name()Renders the `core/comment-author-name` block on the server.
- render_block_core_comment_content()Renders the `core/comment-content` block on the server.
- render_block_core_comment_date()Renders the `core/comment-date` block on the server.
- render_block_core_comment_reply_link()Renders the `core/comment-reply-link` block on the server.
- touch_time()Prints out HTML form date elements for editing post or comment publish date.
- wp_ajax_delete_comment()Handles deleting a comment via AJAX.
- wp_ajax_dim_comment()Handles dimming a comment via AJAX.
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.
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.