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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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' )
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0