Core Data: Add hook to retrieve resource(s) by ID #44900
Labels
[Package] Core data
/packages/core-data
[Type] New API
New API to be used by plugin developers or package users.
What problem does this address?
With
useEntityRecord
, you have to know the subtype of a record, for example the post type if querying bypostType
. This is partially a consequence of the REST API segregating records by post/term type, and providing no method equivalent toget_post
orget_term
which could act across those different post type-specific endpoints. As of WordPress 6.1 the search endpoint supportsinclude
andexclude
parameters (dev note link), which can be used to query for a record knowing nothing more than its object type (e.g.post
orterm
) and its ID.With this new parameter, it is possible to implement a lightweight wrapper around
useEntityRecord
which uses the?include=:id
query parameter on the/wp/v2/search
endpoint to retrieve a record's subtype, and then delegates touseEntityRecord
to fetch the resource directly. This has been useful to us in custom block and block editor plugin development, because we can store only the ID of a related record in our block's attributes and use that ID to reliably query for the right record.There's a sample implementation of this type of wrapper in the same core dev note.
What is your proposed solution?
Introduce a new hook (name TBD, possibly
useEntityRecordById
) which could take the basekind
of record (e.g.postType
) and the ID of the resource, and abstract the need to know thename
(or "subtype") if querying for terms or posts. For example,This hook would only be implemented or useful for a subset of entity
kind
s because the search endpoint does not includeroot
entities.The text was updated successfully, but these errors were encountered: