In the Gutenberg context, getEntityRecords is used to retrieve a collection of entities, usually returned as an array.
If we look into the entities.js file, we can see an array of entities that we can request. We won’t go through each of them, but rather we will see how to use it.
Entities can have a “kind”. For example, the
page entities are of the
postType kind. If there are additional custom post types registered, those will be of the
postType kind as well.
For the entities which have a kind, we request as follows:
However, there are entities which don’t have a kind, such as
media. To request these, we use
root as the kind.
Passing arguments to the request
Entities are related to the WP REST API.
Suppose we want to query a post which has the word “keyboard” in its post title. This is how we can do it:
As you might have already guessed it, if you want to query a post with the following parameters:
- The word “Gutenberg” in the title.
- has author with the ID: 1
- is tagged under Tags with IDs 17, 18
Inside a component or a plugin, it would be as follows: