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

Documentation: Document usage of core entities (getEntityRecords, etc) #12120

Closed
Novalis80 opened this issue Nov 20, 2018 · 11 comments
Closed

Documentation: Document usage of core entities (getEntityRecords, etc) #12120

Novalis80 opened this issue Nov 20, 2018 · 11 comments
Labels
Needs Dev Ready for, and needs developer efforts [Package] Core data /packages/core-data [Type] Developer Documentation Documentation for developers

Comments

@Novalis80
Copy link

Novalis80 commented Nov 20, 2018

I read the docs for "getEntityRecords" and i have no clue what that is nor what is available:

state: State tree
kind: Entity kind
name: Entity name
key: Record’s key 

So if anybody can shed some light on the issue i would be grateful.

My specific task is to get all posts of a certain category into a custom block. I guess this is the method that will get the job done?!

@youknowriad youknowriad added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Nov 20, 2018
@designsimply designsimply added the Needs Technical Feedback Needs testing from a developer perspective. label Nov 20, 2018
@designsimply
Copy link
Member

designsimply commented Jan 31, 2019

I'm moving this out of [Type] Help Request and into [Type] Documentation.

@Novalis80 I found an article with several examples that use getEntityRecords and I don't see one that specifically mentions categories but does this comment asking about tags which might help:

https://riad.blog/2018/06/07/efficient-client-data-management-for-wordpress-plugins/#comment-1077

@designsimply designsimply added [Type] Developer Documentation Documentation for developers and removed [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Jan 31, 2019
@buhlahkay
Copy link

Would love some more documentation on Entities in general.

@aduth
Copy link
Member

aduth commented Apr 22, 2019

My specific task is to get all posts of a certain category into a custom block. I guess this is the method that will get the job done?!

Responding to this specific request, you can achieve this by the following:

wp.data.select( 'core' ).getEntityRecords( 'postType', 'post', { categories: [ /* ... */ ] } )

...where the first two arguments correspond to retrieving entities of a particular post type (source) and the last is an object of query arguments to include in the request.

Note that the first time it is called, it may return an empty result, since it needs to fetch the data from the REST API. You can subscribe to changes in state by either using withSelect or subscribe.

I agree there should be better documentation here.

@aduth aduth removed the Needs Technical Feedback Needs testing from a developer perspective. label Apr 22, 2019
@aduth aduth changed the title getEntityRecords get all post of a category Apr 22, 2019
@aduth aduth added the [Package] Core data /packages/core-data label Apr 25, 2019
@aduth
Copy link
Member

aduth commented Apr 25, 2019

#15200 is a first pass here, which will include formatted documentation in the @wordpress/core-data README. This will simply be adapted directly from the JSDoc of the actions and selectors. As noted there, a next step will be to include useful @example tags.

@towfiqi
Copy link

towfiqi commented Jun 2, 2019

@aduth This only works in the editor page. I am building a custom plugin with React that doesnt invlove gutenberg. I am trying to fetch latest posts , but in my Custom Plugin admin page, when I console log wp.data.select( 'core' ), I get undefined, which works fine in editor pages.

Is this restricted to only on editor pages? How do I use it for another custom page? Do I have to use the rest api?

@aduth
Copy link
Member

aduth commented Jun 3, 2019

@towfiqi If you rely on data from a particular module, you must ensure that your script defines that module as a dependency, using the $deps argument of wp_register_script or wp_enqueue_script.

In your case, core is the core-data module, so your enqueue should look similar to:

wp_enqueue_script( 'my-script', 'my-src.js', array( 'wp-core-data' ) );

All packages are registered in WordPress with the wp- prefix. Most other data stores just use the same name as their package. 'core' is the one exception here (as 'core-data').

@towfiqi
Copy link

towfiqi commented Jun 3, 2019

I was trying array( 'wp-core' ) before. Now it works!! Thanks :)

@gziolo gziolo added the Needs Dev Ready for, and needs developer efforts label Oct 20, 2019
@makkabi
Copy link

makkabi commented Jun 16, 2020

The documentation and event the source code says that getEntityRecords accepts four arguments, the first beeing a 'State Tree' object. But every example I see just uses three arguments, like this:
wp.data.select("core").getEntityRecords('postType', 'post', { per_page: -1 })
What am I missing? How can this even work, when the first argument is supposed to be an object?

@Lovor01
Copy link
Contributor

Lovor01 commented Apr 29, 2021

The documentation and event the source code says that getEntityRecords accepts four arguments, the first beeing a 'State Tree' object. But every example I see just uses three arguments, like this:
wp.data.select("core").getEntityRecords('postType', 'post', { per_page: -1 })
What am I missing? How can this even work, when the first argument is supposed to be an object?

I am late for an answer, but as I understood from reading, useSelect or withSelect inject first argument, i.e. state

@skorasaurus
Copy link
Member

For anyone else still trying to figure this out and what are some basic questions (what is an entity? what are entityrecords? ) #37615 answers some of these questions; Here is the The most recent published version .

@DaisyOlsen
Copy link
Contributor

It looks like this documentation does exist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Dev Ready for, and needs developer efforts [Package] Core data /packages/core-data [Type] Developer Documentation Documentation for developers