Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core Data: Add hook to retrieve resource(s) by ID #44900

Open
kadamwhite opened this issue Oct 12, 2022 · 0 comments
Open

Core Data: Add hook to retrieve resource(s) by ID #44900

kadamwhite opened this issue Oct 12, 2022 · 0 comments
Labels
[Package] Core data /packages/core-data [Type] New API New API to be used by plugin developers or package users.

Comments

@kadamwhite
Copy link
Contributor

What problem does this address?

With useEntityRecord, you have to know the subtype of a record, for example the post type if querying by postType. This is partially a consequence of the REST API segregating records by post/term type, and providing no method equivalent to get_post or get_term which could act across those different post type-specific endpoints. As of WordPress 6.1 the search endpoint supports include and exclude parameters (dev note link), which can be used to query for a record knowing nothing more than its object type (e.g. post or term) 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 to useEntityRecord 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 base kind of record (e.g. postType) and the ID of the resource, and abstract the need to know the name (or "subtype") if querying for terms or posts. For example,

useEntityRecordById( 'postType', 78 );

useEntityRecordById( 'taxonomy', 87 );

This hook would only be implemented or useful for a subset of entity kinds because the search endpoint does not include root entities.

@talldan talldan added [Package] Core data /packages/core-data [Type] New API New API to be used by plugin developers or package users. labels Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Core data /packages/core-data [Type] New API New API to be used by plugin developers or package users.
2 participants