We are using in project Wordpress API for displaying blog posts on frontend in our Nuxt 2 app. The problem is that we would like to create possibility of preview blog post's draft without being logged as admin/moderator. I could not find any entries about post draft preview on https://developer.wordpress.org/rest-api/ or about letting everyone to see them. Anyone got any clues?
The WP REST API allows you to query posts by post type (API endpoint), and filter by post status
.
Check documentation about listing posts.
status
: Limit result set to posts assigned one or more statuses.
type
: Type of Post for the object.
https://example.com/wp-json/wp/v2/posts?status=draft
You can use WP post statuses like publish, future, draft, pending, private
, or custom statuses you made.
Problem is that private
and draft
post statuses are protected: those are not public and cannot be queried without authentication. I just tried and indeed, I can query public posts, but I cannot query private
and draft
posts without my API request being authenticated.
So what you could do:
Authenticate users to the API so they can query draft and private posts
Create a custom post status that is public (public
, publicly_queryable
and exclude_from_search
parameters), so it can be queried without authentication from the API. This way you keep private
and draft
statuses secured like WP wants it. And use another for "drafts that are public".
You can change the "public" status of the private
and draft
post statuses, so they can be queried without authentication (see example). Double-check security concerns if doing so, as it would change the WP core status design.