WP_REST_Posts_Controller::get_item PHP Method

get_item() public method

Retrieves a single post.
Since: 4.7.0
public get_item ( WP_REST_Request $request ) : WP_REST_Response | WP_Error
$request WP_REST_Request Full details about the request.
return WP_REST_Response | WP_Error Response object on success, or WP_Error object on failure.
    public function get_item($request)
    {
        $id = (int) $request['id'];
        $post = get_post($id);
        if (empty($id) || empty($post->ID) || $this->post_type !== $post->post_type) {
            return new WP_Error('rest_post_invalid_id', __('Invalid post ID.'), array('status' => 404));
        }
        $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($id), array('type' => 'text/html'));
        }
        return $response;
    }