Building a custom REST API
Building a REST api via the ASP_Query class
The WordPress built in 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 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.
1
/**
2
Sample payload query arguments:
3
array(
4
's' => 'test',
5
'id' => 1
6
)
7
*/
8
function asp_custom_rest_handler( $data ) {
9
$defaults = $args = array(
10
's' => '',
11
'id' => -1
12
);
13
foreach ( $defaults as $k => $v ) {
14
$param = $data->get_param($k);
15
if ( $param !== null ) {
16
$args[$k] = $param;
17
}
18
}
19
20
$asp_query = new ASP_Query($args, $args['id']);
21
return $asp_query->posts;
22
}
23
24
// POST to: http://example.com/wp-json/ajax-search-pro/v0/search
25
add_action( 'rest_api_init', function () {
26
register_rest_route('ajax-search-pro/v0', '/search', array(
27
'methods' => 'POST',
28
'callback' => 'asp_custom_rest_handler',
29
));
30
});
Copied!

Sample request

1
curl --location -g --request POST 'http://test.local/wp-json/ajax-search-pro/v0/search?s=test&id=1
Copied!
1
$response = wp_safe_remote_post(
2
'http://test.local/wp-json/ajax-search-pro/v0/search',
3
array(
4
'body' => array(
5
's' => 'test',
6
'id' => 1
7
)
8
)
9
);
10
var_dump($response['body']);
Copied!

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.
1
/**
2
Sample payload query arguments:
3
array(
4
's' => 'test',
5
'id' => 1,
6
'post_tax_filter' => array(
7
'category' => array(1, 2),
8
'post_tag' => array(3)
9
)
10
)
11
*/
12
function asp_custom_rest_handler( $data ) {
13
$defaults = $args = array(
14
's' => '',
15
'id' => -1
16
);
17
$params = $data->get_params();
18
foreach ( $defaults as $k => $v ) {
19
if ( isset($params[$k]) && $params[$k] !== null ) {
20
$args[$k] = $params[$k];
21
}
22
}
23
24
// Post taxonomy filter
25
if ( isset($params['post_tax_filter']) ) {
26
$args['post_tax_filter'] = array();
27
foreach ( $params['post_tax_filter'] as $taxonomy => $terms ) {
28
if ( taxonomy_exists($taxonomy) && is_array($terms) && count($terms) ) {
29
$args['post_tax_filter'][] = array(
30
'taxonomy' => $taxonomy,
31
'include' => array_map('intval', $terms),
32
'allow_empty' => false
33
);
34
}
35
}
36
}
37
38
$asp_query = new ASP_Query($args, $args['id']);
39
return $asp_query->posts;
40
}
41
42
// POST to: http://example.com/wp-json/ajax-search-pro/v2/id/1/search
43
add_action( 'rest_api_init', function () {
44
register_rest_route('ajax-search-pro/v0', '/search', array(
45
'methods' => 'POST',
46
'callback' => 'asp_custom_rest_handler',
47
));
48
});
Copied!

Sample request

1
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'
Copied!
1
$response = wp_safe_remote_post(
2
'http://test.local/wp-json/ajax-search-pro/v0/search',
3
array(
4
'body' => array(
5
's' => 'test',
6
'id' => 1,
7
'post_tax_filter' => array(
8
'category' => array(1, 2),
9
'post_tag' => array(3)
10
)
11
)
12
)
13
);
14
var_dump($response['body']);
Copied!