简体   繁体   中英

How to retrieve private posts from wordpress using JSON rest api?

In my wordpress, I have public posts,draft posts and private posts. I installed WP REST API, so that I can access posts from another domain.

I am able to retrieve the public posts easily with following json command

http://www.example.dev/wp-json/posts?_jsonp=?
complete code 
http://codepen.io/anon/pen/YXNrre

But, I could not retrieve private posts and draft posts. I think, I need to pass login information

I also tried

http://www.example.dev/wp-json/posts?type[]=post&_jsonp=?
http://www.example.dev/wp-json/posts?type[]=post&filter[status]=private&_jsonp=?

http://www.example.dev/wp-json/posts?filter[status]=private

How to pass authentication information to server from client side? I have wordpress login and pass word. I don't know php. I think I only need jquery. and how to access private posts from another domain using JSON? thanks in advance.

There is API documentation here. http://wp-api.org/#posts_retrieve-posts

The field that sets the status for posts is called "post_status", not just "status". The default for post_status is generally "publish".

Also, you can only use that field if you are authenticated like you mentioned. There's docs for that here http://wp-api.org/guides/authentication.html

I have the very same issue and

/wp-json/wp/v2/posts?post_status=private 

doesn't filter anything.

For anyone still interested in the above a solution is related to authentication as @iridian mentioned but you'll also need to edit membership roles to allow the user to read private posts.

Required Plugins:

  1. Any JWT auth plugin (for rest API or graphql) (free)
  2. Graphql cors plugin could be helpful as well
  3. Members plugin from memberPress (free)

Process:

  1. Configure JWT auth
  2. Choose the role of the current user and update capabilities to allow for reading private posts.
  3. Authenticate user on the frontend
  4. Pass auth token to request header as an authorization "Bearer token"
  5. Make a request to
const { data } = this.axios.get('https://your-wp-url.com/wp-json/wp/v2/posts?status=private&orderby=date&per_page=10&_embed')

That returns 10 posts with the status of private, ordered by date with images embedded.

Also, Check out the explanation at https://wordpress.stackexchange.com/a/356136/198077

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM