manifest / integrations / rest-api-search-by-id
REST API Search by ID
WordPress plugin to support the "include" and "exclude" parameters on the /search REST API endpoint.
by Human Made · github.com/humanmade/rest-api-search-by-id · website
★ 2stars
13composer installs
0forks
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/humanmade/rest-api-search-by-id/archive/refs/heads/main.zipAlso on Packagist as humanmade/rest-api-search-by-id:
composer require humanmade/rest-api-search-by-idFrom the readme
REST API Search by ID
This plugin modifies the WordPress REST API /wp/v2/search endpoint to support the include and exclude query parameters used in other collections.
In the default REST API you must know the post type for a resource in order to be able to query it, because API routes are broken out by resource type. However, in WordPress we have many functions which accept only an ID and return a matching post regardless of type. With this plugin active it is possible to query for a specific post object by ID, even if you do not know that record's post type:
useResourceById Hook
In addition to the API query parameter changes described above, when this plugin is active there is also a script with the handle use-resource-by-id enqueued while you are using the Block Editor. This script declares a React hook which wraps the WordPress Block Editor's own getEntityRecord selector to let you request a post resource without knowing its post type.
Usage:
This hook uses the search endpoint with the include parameter to determine the subtype of the requested record. That subtype is cached in memory once known, and the hook will thereafter return the results of a direct getEntityRecord