WP_REST_Term_Search_Handler::prepare_item() WordPress Method
The prepare_item() method is used to prepare a single term for search results. This method is called by the WP_REST_Term_Search_Handler class for each term that is matched by the search. The prepare_item() method accepts a single WP_Term object and returns an array of data for that term. The data includes the term's ID, name, slug, and description. The prepare_item() method can be used to add additional data to the search results. The prepare_item() method is called by the WP_REST_Term_Search_Handler class. This class is used to handle searches for terms in the WordPress REST API. The prepare_item() method is used to prepare a single term for search results. This method is called for each term that is matched by the search. The prepare_item() method accepts a single WP_Term object and returns an array of data for that term. The data includes the term's ID, name, slug, and description. The prepare_item() method can be used to add additional data to the search results.
WP_REST_Term_Search_Handler::prepare_item( int $id, array $fields ) #
Prepares the search result for a given ID.
Parameters
- $id
(int)(Required)Item ID.
- $fields
(array)(Required)Fields to include for the item.
Return
(array) Associative array containing all fields for the item.
Source
File: wp-includes/rest-api/search/class-wp-rest-term-search-handler.php
public function prepare_item( $id, array $fields ) { $term = get_term( $id ); $data = array(); if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) { $data[ WP_REST_Search_Controller::PROP_ID ] = (int) $id; } if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) { $data[ WP_REST_Search_Controller::PROP_TITLE ] = $term->name; } if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) { $data[ WP_REST_Search_Controller::PROP_URL ] = get_term_link( $id ); } if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) { $data[ WP_REST_Search_Controller::PROP_TYPE ] = $term->taxonomy; } return $data; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
5.6.0 | Introduced. |