您可以使用rest_names_query
筛选以处理自定义starts_with
REST API参数,并使用terms_clauses
筛选以添加自定义SQL子句,用于搜索名称以指定字母开头的术语(例如。A
或a
对于/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 );