Bring entities on draft mode Strapi

Viewed 1943

Scenario

I'm trying to get all entities from an endpoint, the ones on draft mode, and the ones on published mode. I know that if I want to post on draft mode, I have to post published_at on null on the body request.

If I do:

/posts?published_at_null=true 

that returns an empty array.

Question

How can I do to return ALL the posts?

2 Answers

you will have to create a custom control that will fetch all the entries.you cannot fetch draft data using the existing restapi urls.

const { sanitizeEntity } = require('strapi-utils');

module.exports = {
  async findUnpublished(ctx) {

    //getting all the existing articles, no meter if they have unpublished status
    let result = await strapi.query('posts').find();
    
    //sanitize them to hide all private fields
    let articles = sanitizeEntity(result, {
        model: strapi.models['posts'],
    });
    
    //return result to the /findUnpublished API
    ctx.send(articles);
  }
};
Related