WP_REST_Comments_Controller::get_comment() WordPress Method

The get_comment() method is used to retrieve a single comment from the WordPress database. This method accepts a comment ID as its only parameter. This method returns a WP_Comment object on success. If the comment does not exist, then this method will return false.

WP_REST_Comments_Controller::get_comment( int $id ) #

Get the comment, if the ID is valid.


Parameters

$id

(int)(Required)Supplied ID.


Top ↑

Return

(WP_Comment|WP_Error) Comment object if ID is valid, WP_Error otherwise.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

	protected function get_comment( $id ) {
		$error = new WP_Error(
			'rest_comment_invalid_id',
			__( 'Invalid comment ID.' ),
			array( 'status' => 404 )
		);

		if ( (int) $id <= 0 ) {
			return $error;
		}

		$id      = (int) $id;
		$comment = get_comment( $id );
		if ( empty( $comment ) ) {
			return $error;
		}

		if ( ! empty( $comment->comment_post_ID ) ) {
			$post = get_post( (int) $comment->comment_post_ID );

			if ( empty( $post ) ) {
				return new WP_Error(
					'rest_post_invalid_id',
					__( 'Invalid post ID.' ),
					array( 'status' => 404 )
				);
			}
		}

		return $comment;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.2Introduced.

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.