I've gone through the documentation and the source code but I couldn't understand.
I have 2 queries:
getEntityRecords( 'postType', 'post' );
and getEntityRecords( 'root', 'user' )
.
- Why does the 2nd query require
root
as the 1st argument? - What does it mean?
- How to determine when to use it?
The doc describes the parameters of getEntityRecords
as following:
Parameters:
- state Object: State tree
- kind string: Entity kind.
- name string: Entity name.
- query ?Object: Optional terms query.
- Are
postType
androot
both state trees? - How does
state
relate tokind
while referring to entities.js?
I've gone through the documentation and the source code but I couldn't understand.
I have 2 queries:
getEntityRecords( 'postType', 'post' );
and getEntityRecords( 'root', 'user' )
.
- Why does the 2nd query require
root
as the 1st argument? - What does it mean?
- How to determine when to use it?
The doc describes the parameters of getEntityRecords
as following:
Parameters:
- state Object: State tree
- kind string: Entity kind.
- name string: Entity name.
- query ?Object: Optional terms query.
- Are
postType
androot
both state trees? - How does
state
relate tokind
while referring to entities.js?
1 Answer
Reset to default 3The important thing to note is that post
is a kind/sub-type of the post types entity, all posts regardless of type share the same table and general data structure and fields etc.
This contrasts with other major entity types such as comments or users.
However, what if there is no kind or sub-type? What if your entity is a user? What would you put as the entity kind/sub-type? There is only 1 kind of user! So you go one level up, and that's where root
comes from.
It's also useful to know that entities are very related to the REST API and endpoints, and that this is the only connection to the PHP side of things. Entities only exist in the WP javascript packages. It's possible to use the REST API and javascript without referring to entities on the frontend, but if you're working with the block editor it's highly relevant.