Get custom posts in gutenberg block

Question

I am quite new to block development and react. I am trying to make a block which shows a list of custom posts of choice. I as able to create attributes to save user preferences like post type, count, status, etc. Now I want to fetch the posts. Where I am not sure if should use php endpoint or WP JSON API to fetch posts? I saw in may places they are either using some custom php endpoint or just using the render_callback to call php function which takes care of fetching and showing posts. Is there any downsides using the REST API with JS rather than fetching in good old WP_Query way?

import './editor.scss';
import './style.scss';

const { __ } = wp.i18n; // Import __() from wp.i18n
const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks

registerBlockType( 'cgb/block-myposts', {
    title: __( 'myposts - CGB Block' ),
    icon: 'shield',
    category: 'common',
    keywords: [
        __( 'myposts — CGB Block' ),
        __( 'CGB Example' ),
        __( 'create-guten-block' ),
    ],

    edit: ( props ) => {
        // I want to fetch posts here and show
    },

    save: ( props ) => {
        return null;
    },
} );
0
Imrul.H 2 months 0 Answers 16 views 0

Leave an answer