1

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?

2

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/

| improve this answer | |
  • Sweet. I thought I might. Thank you for confirming. – Stephen Sabatini Jul 15 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.