How to use admin api to create *published* posts?

I am using the javascript admin api to upload content. It uploads fine as a draft, but I would like for it to have published status.

My api call looks like this:

  api.posts
    .add(
      { title: json["EventItemsx"]["EventBodyName"], html },
      { source: "html" },
      { status: "published"}
    )

I got that last parameter from

where it gives this example below, but the result is still a draft post. On displaying the response from the api call, I get:


status: ‘draft’,

Which matches the status in the admin posts screen itself.

From API Documentation:

Required fields: title

Create draft and published posts with the add posts endpoint. All fields except title can be empty or have a default that is applied automatically. Below is a minimal example for creating a published post with content:

// POST /admin/posts/
{
    "posts": [
        {
            "title": "My test post",
            "lexical": "{\"root\":{\"children\":[{\"children\":[{\"detail\":0,\"format\":0,\"mode\":\"normal\",\"style\":\"\",\"text\":\"Hello, beautiful world! 👋\",\"type\":\"extended-text\",\"version\":1}],\"direction\":\"ltr\",\"format\":\"\",\"indent\":0,\"type\":\"paragraph\",\"version\":1}],\"direction\":\"ltr\",\"format\":\"\",\"indent\":0,\"type\":\"root\",\"version\":1}}",
            "status": "published"
        }
    ]
}
1 Like

What happens if you structure your call like this?

api.posts.add(
  { title: json["EventItemsx"]["EventBodyName"], html, status: "published" },
  { source: "html" }
);
2 Likes

Thanks Ryan, that appears to work. But it raises a possibly related issue.

On the posts admin page at http://myipaddress:2368/ghost/#/posts

I am only seeing draft posts and not published posts, even though the selector dropdown says “all posts”. If I chose “published posts”, then my published posts are there.

Is this the expected behavior?

The admin page shows draft posts first, then published, as the default behavior. If you scroll down, are the published posts down below the drafts? :slight_smile:

1 Like

Thanks Cathy. I see there is a dropdown for sort order I hadn’t paid attention to before. It’s all there!

2 Likes