WP_REST_Search_Controller::prepare_item_for_response() public WP 5.0.0
Prepares a single search result for response.
{} It's a method of the class: WP_REST_Search_Controller{}
No Hooks.
Return
WP_REST_Response. Response object.
Usage
$WP_REST_Search_Controller = new WP_REST_Search_Controller(); $WP_REST_Search_Controller->prepare_item_for_response( $id, $request );
- $id(int/string) (required)
- ID of the item to prepare.
- $request(WP_REST_Request) (required)
- Request object.
Changelog
Since 5.0.0 | Introduced. |
Since 5.6.0 | The $id parameter can accept a string. |
Code of WP_REST_Search_Controller::prepare_item_for_response() WP REST Search Controller::prepare item for response WP 5.6
public function prepare_item_for_response( $id, $request ) {
$handler = $this->get_search_handler( $request );
if ( is_wp_error( $handler ) ) {
return new WP_REST_Response();
}
$fields = $this->get_fields_for_response( $request );
$data = $handler->prepare_item( $id, $fields );
$data = $this->add_additional_fields_to_object( $data, $request );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
$links = $handler->prepare_item_links( $id );
$links['collection'] = array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
);
$response->add_links( $links );
return $response;
}