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?
1 Answer
You'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.org/block-editor/packages/packages-api-fetch/