> For the complete documentation index, see [llms.txt](https://knowledgebase.ajaxsearchpro.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://knowledgebase.ajaxsearchpro.com/other/building-a-custom-rest-api.md).

# Building a custom REST API

The [WordPress built in REST API](https://developer.wordpress.org/rest-api/) supports extension via the ***register\_rest\_route*** function. We can use that to build a custom REST interface to access the search results.

In the following examples the [ASP\_Query](/api/searchquery.md) class is used in the REST API handler functions.

### Simple search example

In this example a simple search is executed by sending the "s" and "id" arguments to the \
`/wp-json/ajax-search-pro/v0/search` endpoint via a **POST** request.

[What is this, and where do I put this custom code?](/safe-coding-guideline.md)

```php
/**
Sample payload query arguments: 
array(
	's' => 'test',
	'id' => 1
)
*/
function asp_custom_rest_handler( $data ) {
	$defaults = $args = array(
		's' => '',
		'id' => -1
	);
	foreach ( $defaults as $k => $v ) {
		$param = $data->get_param($k);
		if ( $param !== null ) {
			$args[$k] = $param;
		}
	}

	$asp_query = new ASP_Query($args, $args['id']);
	return $asp_query->posts;
}

// POST to: http://example.com/wp-json/ajax-search-pro/v0/search
add_action( 'rest_api_init', function () {
	register_rest_route('ajax-search-pro/v0', '/search', array(
		'methods' => 'POST',
		'callback' => 'asp_custom_rest_handler',
	));
});
```

#### Sample request

```
curl --location -g --request POST 'http://test.local/wp-json/ajax-search-pro/v0/search?s=test&id=1
```

```php
$response = wp_safe_remote_post(
   'http://test.local/wp-json/ajax-search-pro/v0/search',
   array(
	'body' => array(
		's' => 'test',
		'id' => 1
	)
   )
);
var_dump($response['body']);
```

### Search with post taxonomy filters

In this example a simple search is executed by sending the "s" and "id" as well as "post\_tax\_filter" arguments to the \
`/wp-json/ajax-search-pro/v0/search` endpoint via a **POST** request.

[What is this, and where do I put this custom code?](/safe-coding-guideline.md)

```php
/**
Sample payload query arguments: 
array(
	's' => 'test',
	'id' => 1,
	'post_tax_filter' => array(
		'category' => array(1, 2),
		'post_tag' => array(3)
	)
)
*/
function asp_custom_rest_handler( $data ) {
	$defaults = $args = array(
		's' => '',
		'id' => -1
	);
	$params = $data->get_params();
	foreach ( $defaults as $k => $v ) {
		if ( isset($params[$k]) && $params[$k] !== null ) {
			$args[$k] = $params[$k];
		}
	}

	// Post taxonomy filter
	if ( isset($params['post_tax_filter']) ) {
		$args['post_tax_filter'] = array();
		foreach ( $params['post_tax_filter'] as $taxonomy => $terms ) {
			if ( taxonomy_exists($taxonomy) && is_array($terms) && count($terms) ) {
				$args['post_tax_filter'][] = array(
					'taxonomy' => $taxonomy,
					'include'  => array_map('intval', $terms),
					'allow_empty' => false
				);
			}
		}
	}

	$asp_query = new ASP_Query($args, $args['id']);
	return $asp_query->posts;
}

// POST to: http://example.com/wp-json/ajax-search-pro/v2/id/1/search
add_action( 'rest_api_init', function () {
	register_rest_route('ajax-search-pro/v0', '/search', array(
		'methods' => 'POST',
		'callback' => 'asp_custom_rest_handler',
	));
});
```

#### Sample request

```
curl --location -g --request POST 'http://test.local/wp-json/ajax-search-pro/v0/search?s=test&id=1&post_tax_filter[category][]=1&post_tax_filter[category][]=2&post_tax_filter[post_tag][]=3'
```

```php
$response = wp_safe_remote_post(
   'http://test.local/wp-json/ajax-search-pro/v0/search',
   array(
	'body' => array(
		's' => 'test',
		'id' => 1,
		'post_tax_filter' => array(
			'category' => array(1, 2),
			'post_tag' => array(3)
		)
	)
   )
);
var_dump($response['body']);
```
