Skip to content

Instantly share code, notes, and snippets.

@craigiswayne
Created July 15, 2017 12:16
Show Gist options
  • Select an option

  • Save craigiswayne/61e09a4b5fa96904d663e2bffa33e718 to your computer and use it in GitHub Desktop.

Select an option

Save craigiswayne/61e09a4b5fa96904d663e2bffa33e718 to your computer and use it in GitHub Desktop.
WordPress Javascript REST API Example
var collection = new wp.api.collections.Posts();
collection.fetch().done(function(response) {
console.log(response);
console.log('| ID | Author | Date | Title |');
console.log('|-------|--------|---------------------|--------|');
for ( var i = 0; i < response.length; i++) {
var row = '| ' + response[i].id + ' | ' + response[i].author + ' | ' + response[i].date + ' | '+ response[i].title.rendered;
console.log(row);
}
console.log('|-------|--------|---------------------|--------|');
});
/**
* Example Output
Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
(10) [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
| ID | Author | Date | Title |
|-------|--------|---------------------|--------|
| 42572 | 3422 | 2017-06-06T17:00:15 | Title here....
| 42594 | 3422 | 2017-06-06T16:54:53 | Title here....
| 42351 | 3422 | 2017-06-06T12:27:42 | Title here....
| 42520 | 3420 | 2017-06-06T12:06:48 | Title here....
| 42575 | 3420 | 2017-06-06T09:59:11 | Title here....
| 42566 | 3422 | 2017-06-06T09:58:18 | Title here....
| 42581 | 3422 | 2017-06-06T09:52:38 | Title here....
| 42489 | 3422 | 2017-06-05T17:19:41 | Title here....
| 42538 | 3420 | 2017-06-05T17:18:15 | Title here....
| 42403 | 3420 | 2017-06-05T17:17:44 | Title here....
|-------|--------|---------------------|--------|
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment