Create Multiple Posts At Once
What is interesting here is that if you try to google the title of this post you will find a lot of plugins and most of them are outdated and no articles or code examples.
In this tutorial I am going to guide you through the process of bulk creating posts, we will not only create posts or pages automatically, but also try to use the data from Lorem Ipsum generators or Open AI (ChatGPT).
By the way, if you need to bulk copy posts from one WordPress website to another, in this case I can recommend you my plugin (or its multisite version).
Generating Posts Using wp_insert_post()
The question is here, should we really use wp_insert_post()
for that purpose? This function does a lot of things, so maybe a faster way will be INSERT INTO
SQL query?
And what I think here is while wp_insert_post()
could be slower than a single SQL query, the things this function does are really useful:
- You will have a simpler code, especially in case you would like to add categories and metadata to posts.
- If you have some plugins running on your site, you can be sure they will be compatible with newly created posts.
Ok, let’s dive into a simple example:
$number_of_posts = 10;
for( $i = 0; $i < $number_of_posts; $i++ ) {
wp_insert_post(
array(
'post_title' => 'Article ' . ( $i + 1 ),
'post_content' => 'Some content',
'post_status' => 'draft'
)
);
}
Once running this code we will get a result like this:

The only thing, please don’t run wp_insert_post()
function directly in functions.php
because it may be too early. If you are testing stuff this way, just add admin_init
hook or something.
Now I am going to show you how to create multiple posts at once using a list of post titles.
$titles = array(
'Where to work with laptop in Athens?',
'Coffee Guide to Istanbul',
'Snowboarding in Georgia',
'How to bulk publish posts with WordPress REST API'
);
foreach( $titles as $title ) {
wp_insert_post(
array(
'post_title' => $title,
'post_status' => 'publish' // let's publish posts immediately
)
);
}
And then we will have a new list of published posts. Seems quite easy, doesn’t it?

The last but not the least, let’s make our example more interesting, in order to do that we will try to add categories, tags and custom fields to our posts.
$articles = array(
array(
'title' => 'Where to work with laptop in Athens?',
'city' => 'Athens',
),
array(
'title' => 'Coffee Guide to Istanbul',
'city' => 'Istanbul',
),
array(
'title' => 'Snowboarding in Georgia',
'city' => 'Gudauri',
),
array(
'title' => 'How to bulk publish posts with WordPress REST API',
)
);
foreach( $articles as $article ) {
$article_id = wp_insert_post(
array(
'post_title' => $article[ 'title' ],
'post_status' => 'publish'
)
);
if( ! empty( $article[ 'city' ] ) ) {
update_post_meta( $article_id, 'city', $article[ 'city' ] );
wp_set_post_terms( $article_id, 'travel', 'post_tag' );
}
}
Using Lorem Ipsum Generators to Create WordPress Posts
Let’s now create multiple posts but generate either post content or excerpts with the help of any Lorem Ipsum generator API. I decided to use this one dinoipsum.com it seems not boring.
I am going to use a modified version of the first example here.
$number_of_dinosaurus = 5;
for( $i = 0; $i < $number_of_dinosaurus; $i++ ) {
$request = wp_remote_get(
add_query_arg(
array(
'format' => 'text',
'paragraphs' => 1,
'words' => 15,
),
'https://dinoipsum.com/api/'
)
);
if( 'OK' !== wp_remote_retrieve_response_message( $request ) ) {
continue;
}
wp_insert_post(
array(
'post_title' => 'Dinosaur ' . ( $i + 1 ),
'post_excerpt' => wp_remote_retrieve_body( $request ),
)
);
}
The only thing I would like to highlight that probably it is better to run one HTTP request to Lorem Ipsum generate and then split the content between all the posts that we are going to bulk create. Actually it is what I am going to do in the next chapter about Open AI.
But for now, here is the result:

Generate WordPress Posts with Open AI (ChatGPT)
But what is the point to use lorem ipsum content if we can use AI-generated content now? Let’s find it out.
In the example below I am going to use Open AI API. Of course first of all you have to sign up there and to create an API key. Let’s ask Open AI to generate 5 titles about dinosaurs, we are going to use them as our post titles.
$text = 'Write 5 taglines about dinosaurs.';
$password = 'API KEY IS HERE';
$chatgpt_request = wp_remote_post(
'https://api.openai.com/v1/chat/completions',
array(
'timeout' => 30,
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $password
),
'body' => json_encode(
array(
'model' => 'gpt-3.5-turbo',
'messages' => array(
array(
'role' => 'user',
'content' => $text
),
),
'temperature' => 0.7 // randomeness
)
)
)
);
if( 'OK' === wp_remote_retrieve_response_message( $chatgpt_request ) ) {
$body = json_decode( wp_remote_retrieve_body( $chatgpt_request ) );
// by default we have it in a format like
// 1. "First title"
// 2. "Second title"
// 3. ...
// let's do some formatting
$titles = array_map(
function( $choice_text ) {
if( preg_match( '/"([^"]+)"/', $choice_text, $c ) ) {
return $c[1];
}
},
explode( "\n", trim( $body->choices[0]->message->content ) )
);
foreach( $titles as $title ) {
wp_insert_post(
array(
'post_title' => $title
)
);
}
}
- As you can see, nothing fancy here, I am just using WordPress HTTP API to make a request to Open AI API and to process the result from there.
- On line
7
I set'timeout' => 30
, which is extremely important cause today Open AI API is kind of slow, so we have to let our HTTP request to wait for the result. - The code on lines
31-43
probably is waiting for improvement, because I just made some quick formatting in order to convert the Open AI response into an array of titles what we can use aswp_insert_post()
argument.

By the way, if you need some custom development with Open AI, our team already have experience with it, so, we are here to help.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me