-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Description
What problem does this address?
I've been trying to better understand how to register a custom endpoint added in PHP and make it available to use in the block editor. I've developed a good understanding of how to fetch data with useSelect() from existing data sources such as posts or pages. But I need to go beyond that in cases where the existing endpoints don't give us the option of returning data we need or querying in complex ways.
For instance let's say I need to query for something more complex such as page posts with a specific template applied that also have another specific post meta field entered and perhaps are also tagged with a specific term. In PHP I can do this with WP_Query. I've been trying to learn how to do the same in the block editor.
One approach I've been investigating is registering my own custom endpoint and then figuring out how to access that endpoint in the block editor with useSelect() and getEntityRecords.
I could not find much in the way of official documentation about this. Nor much in the community. I was however able to use some AI tools to experiment and put together a working prototype. It seems the key is using addEntities from core/data package to register the custom endpoint.
But I can't find much documentation on addEntities and I'm not sure I would have been able to figure it out relying just on the docs.
What is your proposed solution?
As someone trying to learn this and migrate my knowledge from the PHP side to the React/JS side I'd like to see more documentation in the core data package and specifically with addEntities and how to make custom endpoints available in the editor to use with existing data functions.
I also am unsure how best or when is best to actually call or dispatch a entity config to addEntities. I currently have it in a separate JS file enqueued with the admin_enqueue_scripts hook and use a subscribe approach to subscribe to the coreStore and then dispatch an array of entities like so:
dispatch(coreStore).addEntities(entitiesToRegister);
Maybe also some more documentation on Entities as a concept?