You can use the rest_names_query
filter to handle the custom starts_with
REST API parameter, and use the terms_clauses
filter to add a custom SQL clause for searching terms having their name starting with the specified letter (e.g. A
or a
for /wp/v2/names/?starts_with=A
):
add_filter( 'rest_names_query', function( $args, $request ){
if ( '/wp/v2/names' === $request->get_route() && // check the route
( $starts_with = $request->get_param( 'starts_with' ) ) ) {
$args['name_starts_with'] = $starts_with;
}
return $args;
}, 10, 2 );
add_filter( 'terms_clauses', function( $clauses, $taxonomies, $args ){
if ( ! empty( $args['name_starts_with'] ) ) {
global $wpdb;
// "t" below is an alias for the WordPress terms table (wp_terms), and it is set by WordPress.
$where = $wpdb->prepare( 't.name LIKE %s', $wpdb->esc_like( $args['name_starts_with'] ) . '%' );
$clauses['where'] .= " AND $where";
}
return $clauses;
}, 10, 3 );