最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest api - Is it posible to use wp.data.select('core') outside a block?

programmeradmin0浏览0评论

I have an options page I have created for a post type which doesn't use a Guttenberg block, but I was wondering if inside that page I could somehow make use @wordpress/data in order to retrieve posts with the below instead of using the REST API?

wp.data.select( 'core' ).getEntityRecords( 'postType', 'post' )

Right now, wp.data.select('core') simply returns null

I have an options page I have created for a post type which doesn't use a Guttenberg block, but I was wondering if inside that page I could somehow make use @wordpress/data in order to retrieve posts with the below instead of using the REST API?

wp.data.select( 'core' ).getEntityRecords( 'postType', 'post' )

Right now, wp.data.select('core') simply returns null

Share Improve this question asked Jan 14, 2021 at 11:44 zanonazanona 1254 bronze badges 1
  • getEntityRecords uses the REST API – Tom J Nowell Commented Jan 14, 2021 at 11:56
Add a comment  | 

1 Answer 1

Reset to default 1

Sorry, you can not use it outside of Guttenberg block.

but you can get same result every where in wp-admin by using the following solution,

For Detail visit the WP Official Documentation

this.posts = {};
this.posts = new wp.api.models.Post();
this.posts.fetch().then( response => {
   console.log(response);
});

The following is an example how i have used it on my pluging custom testing page which was developed on gutenberg components.

  import {render,Component} from "@wordpress/element";

    class TestingPage extends Component {
            constructor() {
                super( ...arguments );

                // Solution - Start 
                this.posts = {};
                this.posts = new wp.api.models.Post();
                this.posts.fetch().then( response => {
                   console.log(response);
                });
                // Solution - End

        }

        render(
            <TestingPage/>,
            document.getElementById( 'testing-page' )
        );
发布评论

评论列表(0)

  1. 暂无评论