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

Retrieves comment data given a comment ID or comment object.


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


Parameters Parameters

$comment

(WP_Comment|string|int) (Optional) Comment to retrieve.

Default value: null

$output

(string) (Optional) 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 value: OBJECT


Top ↑

Return Return

(WP_Comment|array|null) Depends on $output value.


Top ↑

Source Source

File: wp-includes/comment.php

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 mixed $_comment Comment data.
	 */
	$_comment = apply_filters( 'get_comment', $_comment );

	if ( $output == OBJECT ) {
		return $_comment;
	} elseif ( $output == ARRAY_A ) {
		return $_comment->to_array();
	} elseif ( $output == ARRAY_N ) {
		return array_values( $_comment->to_array() );
	}
	return $_comment;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Example
    To get the author’s name of a comment with ID 7:

    <?php
    $my_id = 7;
    $comment_id_7 = get_comment( $my_id ); 
    $name = $comment_id_7->comment_author;
    ?>
    
    Alternatively, specify the <code>$output</code> parameter:
    
    
    <?php
    $my_id = 7;
    $comment_id_7 = get_comment( $my_id, ARRAY_A );
    $name = $comment_id_7['comment_author'];
    ?>
    
    <?php
    //	Correct: pass a dummy variable as post_id
    $the_comment = & get_comment( $dummy_id = 7 );
    	
    //	Incorrect: literal integer as post_id
    $the_comment = & get_comment( 7 );
    //	Fatal error: 'Only variables can be passed for reference' or 'Cannot pass parameter 1 by reference'
    ?>
    
    
  2. Skip to note content
    Contributed by Codex

    Example
    To get the author’s name of a comment with ID 7:

    <?php
    $my_id = 7;
    $comment_id_7 = get_comment( $my_id ); 
    $name = $comment_id_7->comment_author;
    ?>
    

    Alternatively, specify the $output parameter:

    <?php
    $my_id = 7;
    $comment_id_7 = get_comment( $my_id, ARRAY_A );
    $name = $comment_id_7['comment_author'];
    ?>
    
    <?php
    //	Correct: pass a dummy variable as post_id
    $the_comment = & get_comment( $dummy_id = 7 );
    	
    //	Incorrect: literal integer as post_id
    $the_comment = & get_comment( 7 );
    //	Fatal error: 'Only variables can be passed for reference' or 'Cannot pass parameter 1 by reference'
    ?>
    
    

You must log in before being able to contribute a note or feedback.