Trying to get latest 10 posts to display in a Gutenberg plugin, but I can't figure out how to access the latest posts from the block.js. I know how to do it from PHP and it's easy, but this is new to me. Do I have to make a request to the REST API or is there a better practice?
Trying to get latest 10 posts to display in a Gutenberg plugin, but I can't figure out how to access the latest posts from the block.js. I know how to do it from PHP and it's easy, but this is new to me. Do I have to make a request to the REST API or is there a better practice?
Share Improve this question asked Jul 14, 2020 at 15:55 Stephen SabatiniStephen Sabatini 2363 silver badges13 bronze badges 1 |1 Answer
Reset to default 3You'll need to use the REST API. You can access the latest posts by calling:
wp.apiFetch( { path: '/wp/v2/posts' } ).then( posts => {
console.log( posts );
} );
Here is some more information on using the package - https://developer.wordpress/block-editor/packages/packages-api-fetch/
wp.api
developer.wordpress/rest-api/reference/posts in this casewp.api.posts
, I'm about to test this myself – Caleb Jay Commented Oct 17, 2022 at 5:40