Looking for a function for generating posts. how in the sourcecode are posts generated? smth like testUtils.DataGenerator.forKnex.createPost

I want to have a post generated and added. I found in the source testUtils.DataGenerator.forKnex.createPost

is there a similar function for non test? for real life production environment.
So im looking for a function that takes some parameters like user id and post content and add this to the DB.
I’m sure there is a function like this and this can be done easily :slight_smile:

The Admin API has support for creating posts. I think you can specify the owner, but I’m not sure

When using an API Key authentication or user authentication with the editor/admin/owner role then you’re able to set the post authors to any users.

1 Like

yeah, i was thinking about this, but one functioncall like testUtils.DataGenerator.forKnex.createPost is easier and quicker then setting the whole API Key authentication and editor/admin/owner role thing up, that was exactly what i tried to omit :slight_smile:

or in the worst case something like:
but functioncalls would be nicer

const post = testUtils.DataGenerator.forKnex.createPost({
id: ObjectId.generate(),
title: ‘Post with email-only card’,
slug: ‘email-only-card’,
mobiledoc: ‘{“version”:“0.3.1”,“atoms”:,“cards”:[[“email”,{“html”:“

Hey {first_name \“there\”} {unknown}

Welcome to your first Ghost email!


Another email card with a similar replacement, {first_name, \“see?\”}

”}]],“markups”:,“sections”:[[10,0],[1,“p”,[[0,,0,“This is the actual post content…”]]],[10,1],[1,“p”,]]}’,
html: ‘

This is the actual post content…

plaintext: ‘This is the actual post content…’,
status: ‘draft’,
uuid: ‘d52c42ae-2755-455c-80ec-70b2ec55c904’
        return models.Post.add(post, {context: {internal: true}}).then(() => {`

this seems to do what im looking for

1 Like

Any updates on this? I’m looking for the same thing.

The correct solution is to use the API.

Even if I’m trying to generate posts from within the main core Node app?

Even if I’m trying to generate posts from within the main core Node app?

There shouldn’t be anything generating posts from within Ghost outside of tests. Perhaps you could explain the end goal of what you’re trying to achieve?

I’m modifying/customizing it for me needs. Getting posts via xml-files dropped on the server, that I want put in the database. I guess, I could do it outside the main app too.

That would be preferable as it means you don’t have to re-apply changes every time you want to upgrade Ghost and you’re not subject to the internals changing as you’re building against a published+supported API.