Title: WP_REST_Search_Controller::get_search_handler
Published: December 6, 2018
Last modified: February 24, 2026

---

# WP_REST_Search_Controller::get_search_handler( WP_REST_Request $request ): 󠀁[WP_REST_Search_Handler](https://developer.wordpress.org/reference/classes/wp_rest_search_handler/)󠁿|󠀁[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_search_handler/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_search_handler/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_search_handler/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_search_handler/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_search_handler/?output_format=md#changelog)

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

Gets the search handler to handle the current request.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_search_handler/?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_search_handler/?output_format=md#return)󠁿

 [WP_REST_Search_Handler](https://developer.wordpress.org/reference/classes/wp_rest_search_handler/)
|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) Search 
handler for the request type, 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_search_handler/?output_format=md#source)󠁿

    ```php
    protected function get_search_handler( $request ) {
    	$type = $request->get_param( self::PROP_TYPE );

    	if ( ! $type || ! is_string( $type ) || ! isset( $this->search_handlers[ $type ] ) ) {
    		return new WP_Error(
    			'rest_search_invalid_type',
    			__( 'Invalid type parameter.' ),
    			array( 'status' => 400 )
    		);
    	}

    	return $this->search_handlers[ $type ];
    }
    ```

[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/6.9.4/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php#L398)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php#L398-L410)

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

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

Retrieves the translation of $text.

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

Initializes the error.

  |

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

Retrieves a collection of search results.

  | 
| [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.

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

Sanitizes the list of subtypes, to ensure only subtypes of the passed type are included.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_search_controller/get_search_handler/?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_search_handler%2F)
before being able to contribute a note or feedback.