Title: WP_REST_Search_Controller::get_items
Published: December 6, 2018
Last modified: May 20, 2026

---

# WP_REST_Search_Controller::get_items( WP_REST_Request $request ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#wp--skip-link--target)

Retrieves a collection of search results.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#parameters)󠁿

 `$request`[WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
required

Full details about the request.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) Response
object on success, or [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
object on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#source)󠁿

    ```php
    public function get_items( $request ) {
    	$handler = $this->get_search_handler( $request );
    	if ( is_wp_error( $handler ) ) {
    		return $handler;
    	}

    	$result = $handler->search_items( $request );

    	if ( ! isset( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! is_array( $result[ WP_REST_Search_Handler::RESULT_IDS ] ) || ! isset( $result[ WP_REST_Search_Handler::RESULT_TOTAL ] ) ) {
    		return new WP_Error(
    			'rest_search_handler_error',
    			__( 'Internal search handler error.' ),
    			array( 'status' => 500 )
    		);
    	}

    	$ids = $result[ WP_REST_Search_Handler::RESULT_IDS ];

    	$is_head_request = $request->is_method( 'HEAD' );
    	if ( ! $is_head_request ) {
    		$results = array();

    		foreach ( $ids as $id ) {
    			$data      = $this->prepare_item_for_response( $id, $request );
    			$results[] = $this->prepare_response_for_collection( $data );
    		}
    	}

    	$total     = (int) $result[ WP_REST_Search_Handler::RESULT_TOTAL ];
    	$page      = (int) $request['page'];
    	$per_page  = (int) $request['per_page'];
    	$max_pages = (int) ceil( $total / $per_page );

    	if ( $page > $max_pages && $total > 0 ) {
    		return new WP_Error(
    			'rest_search_invalid_page_number',
    			__( 'The page number requested is larger than the number of pages available.' ),
    			array( 'status' => 400 )
    		);
    	}

    	$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $results );
    	$response->header( 'X-WP-Total', $total );
    	$response->header( 'X-WP-TotalPages', $max_pages );

    	$request_params = $request->get_query_params();
    	$base           = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

    	if ( $page > 1 ) {
    		$prev_link = add_query_arg( 'page', $page - 1, $base );
    		$response->link_header( 'prev', $prev_link );
    	}
    	if ( $page < $max_pages ) {
    		$next_link = add_query_arg( 'page', $page + 1, $base );
    		$response->link_header( 'next', $next_link );
    	}

    	return $response;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php#L127)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php#L127-L185)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_REST_Search_Controller::get_search_handler()](https://developer.wordpress.org/reference/classes/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.

  | 
| [WP_REST_Search_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php` |

Prepares a single search result for response.

  | 
| [urlencode_deep()](https://developer.wordpress.org/reference/functions/urlencode_deep/)`wp-includes/formatting.php` |

Navigates through an array, object, or scalar, and encodes the values to be used in a URL.

  | 
| [rest_ensure_response()](https://developer.wordpress.org/reference/functions/rest_ensure_response/)`wp-includes/rest-api.php` |

Ensures a REST response is a response object (for consistency).

  | 
| [rest_url()](https://developer.wordpress.org/reference/functions/rest_url/)`wp-includes/rest-api.php` |

Retrieves the URL to a REST endpoint.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [add_query_arg()](https://developer.wordpress.org/reference/functions/add_query_arg/)`wp-includes/functions.php` |

Retrieves a modified URL query string.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 6 more](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_items/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.0.0](https://developer.wordpress.org/reference/since/5.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_search_controller%2Fget_items%2F)
before being able to contribute a note or feedback.