TransWikia.com

How to grab posts in Gutenberg Block?

WordPress Development Asked by Stephen Sabatini on October 30, 2021

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?

One 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/

Answered by Welcher on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP