WP_REST_Posts_Controller::get_item()publicWP 4.7.0

Retrieves a single post.

Method of the class: WP_REST_Posts_Controller{}

No Hooks.

Return

WP_REST_Response|WP_Error. Response object on success, or WP_Error object on failure.

Usage

$WP_REST_Posts_Controller = new WP_REST_Posts_Controller();
$WP_REST_Posts_Controller->get_item( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 4.7.0 Introduced.

WP_REST_Posts_Controller::get_item() code WP 6.6.2

public function get_item( $request ) {
	$post = $this->get_post( $request['id'] );
	if ( is_wp_error( $post ) ) {
		return $post;
	}

	$data     = $this->prepare_item_for_response( $post, $request );
	$response = rest_ensure_response( $data );

	if ( is_post_type_viewable( get_post_type_object( $post->post_type ) ) ) {
		$response->link_header( 'alternate', get_permalink( $post->ID ), array( 'type' => 'text/html' ) );
	}

	return $response;
}