admin管理员组文章数量:1287840
I’m making a webapp based on wordpress in which I’m using the REST API for my users to get and create posts.
I found the very useful REST API backbone javascript client which seems great. However the documentation in this link is limited to only a few selected examples. Is there a more complete documentation? With more examples?
I'm trying to filter posts by author and status. To display current user's drafts and published posts in different places.
How do you use the 'options'? I can't understand where to filter by the author nor status. For the author I tried a lot of variations like this one but it always return all the posts.
new wp.api.collections.Posts( { data: { 'author': { 'id': '1' }} } ).fetch().then( posts => {
for(const post of posts){
//do stuff with post
}
} );
Beside if I leave out options, it doesn't return the draft posts, only published posts. Thanks!!
I’m making a webapp based on wordpress in which I’m using the REST API for my users to get and create posts.
I found the very useful REST API backbone javascript client which seems great. However the documentation in this link is limited to only a few selected examples. Is there a more complete documentation? With more examples?
I'm trying to filter posts by author and status. To display current user's drafts and published posts in different places.
How do you use the 'options'? I can't understand where to filter by the author nor status. For the author I tried a lot of variations like this one but it always return all the posts.
new wp.api.collections.Posts( { data: { 'author': { 'id': '1' }} } ).fetch().then( posts => {
for(const post of posts){
//do stuff with post
}
} );
Beside if I leave out options, it doesn't return the draft posts, only published posts. Thanks!!
Share Improve this question asked Sep 10, 2021 at 11:55 PaddleStrokePaddleStroke 312 bronze badges 3 |1 Answer
Reset to default 2Thanks @sallyCJ for fixing my error ! Here's some examples for WP-API backbone JS client. It may help others :
Fetch post of a certain author and category :
var filteredPosts = new wp.api.collections.Posts();
authorsPosts.fetch({ data: { author: currentUserId, categories: 42 } }).then( posts => {
for(const post of posts){
//do stuff with each post
}
} );
Update a post :
var post = new wp.api.models.Post( {
id: idOfThePostYouWantToUpdate,
title: newTitle,
content: newContent,
status: newStatus,
categories: [ 42 ],
tags: [ 50 ]
});
post.save();
本文标签: javascriptwpapi Backbone JS Client fetch options
版权声明:本文标题:javascript - wp-api Backbone JS Client fetch options 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741327795a2372588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
fetch()
method and not the constructor. So for example,( new wp.api.collections.Posts() ).fetch( { data: { author: 1 } } )
– Sally CJ Commented Sep 10, 2021 at 14:06