WP_Comment::get_instance() WordPress Method

The WP_Comment::get_instance() method is used to get a WP_Comment instance from a given comment ID. This is useful for retrieving specific comment objects from a database.

WP_Comment::get_instance( int $id ) #

Retrieves a WP_Comment instance.


Parameters

$id

(int)(Required)Comment ID.


Top ↑

Return

(WP_Comment|false) Comment object, otherwise false.


Top ↑

Source

File: wp-includes/class-wp-comment.php

	public static function get_instance( $id ) {
		global $wpdb;

		$comment_id = (int) $id;
		if ( ! $comment_id ) {
			return false;
		}

		$_comment = wp_cache_get( $comment_id, 'comment' );

		if ( ! $_comment ) {
			$_comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id ) );

			if ( ! $_comment ) {
				return false;
			}

			wp_cache_add( $_comment->comment_ID, $_comment, 'comment' );
		}

		return new WP_Comment( $_comment );
	}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.