On this page
WP_REST_Search_Controller::prepare_item_for_response( int|string $item, WP_REST_Request $request ): WP_REST_Response
Prepares a single search result for response.
Parameters
$itemint|string Required-
ID of the item to prepare.
$requestWP_REST_Request Required-
Request object.
Return
WP_REST_Response Response object.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php. View all references
public function prepare_item_for_response( $item, $request ) {
// Restores the more descriptive, specific name for use within this method.
$item_id = $item;
$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( $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 );
if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$links = $handler->prepare_item_links( $item_id );
$links['collection'] = array(
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
);
$response->add_links( $links );
}
return $response;
}
Related
Uses
| Uses | Description |
|---|---|
| rest_is_field_included() wp-includes/rest-api.php | Given an array of fields to include in a response, some of which may be |
| WP_REST_Search_Controller::get_search_handler() wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php | Gets the search handler to handle the current request. |
| rest_ensure_response() wp-includes/rest-api.php | Ensures a REST response is a response object (for consistency). |
| rest_url() wp-includes/rest-api.php | Retrieves the URL to a REST endpoint. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Search_Controller::get_items() wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php | Retrieves a collection of search results. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_search_controller/prepare_item_for_response