REST API: Add support for searching resources by id.

This brings support for the `include` and `exclude` collection parameters to the Search Controller. This can be used to find an item by id when it's subtype is unknown.

Props kadamwhite.
Fixes #56546.

Built from https://develop.svn.wordpress.org/trunk@54123


git-svn-id: http://core.svn.wordpress.org/trunk@53682 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
TimothyBlynJacobs 2022-09-11 21:12:11 +00:00
parent 8f390b309a
commit fc4485c17f
4 changed files with 35 additions and 1 deletions

View File

@ -331,6 +331,24 @@ class WP_REST_Search_Controller extends WP_REST_Controller {
'sanitize_callback' => array( $this, 'sanitize_subtypes' ), 'sanitize_callback' => array( $this, 'sanitize_subtypes' ),
); );
$query_params['exclude'] = array(
'description' => __( 'Ensure result set excludes specific IDs.' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
);
$query_params['include'] = array(
'description' => __( 'Limit result set to specific IDs.' ),
'type' => 'array',
'items' => array(
'type' => 'integer',
),
'default' => array(),
);
return $query_params; return $query_params;
} }

View File

@ -69,6 +69,14 @@ class WP_REST_Post_Search_Handler extends WP_REST_Search_Handler {
$query_args['s'] = $request['search']; $query_args['s'] = $request['search'];
} }
if ( ! empty( $request['exclude'] ) ) {
$query_args['post__not_in'] = $request['exclude'];
}
if ( ! empty( $request['include'] ) ) {
$query_args['post__in'] = $request['include'];
}
/** /**
* Filters the query arguments for a REST API search request. * Filters the query arguments for a REST API search request.
* *

View File

@ -70,6 +70,14 @@ class WP_REST_Term_Search_Handler extends WP_REST_Search_Handler {
$query_args['search'] = $request['search']; $query_args['search'] = $request['search'];
} }
if ( ! empty( $request['exclude'] ) ) {
$query_args['exclude'] = $request['exclude'];
}
if ( ! empty( $request['include'] ) ) {
$query_args['include'] = $request['include'];
}
/** /**
* Filters the query arguments for a REST API search request. * Filters the query arguments for a REST API search request.
* *

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.1-alpha-54122'; $wp_version = '6.1-alpha-54123';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.