I need to insert about 300 posts into the DB programmatically.

Usually i use wp_insert_post to do this job with one record each time, but this time I don't think it'll work well with hundreds of rows.

According to this, it will probably be faster to insert multiple rows in one query. So, I wonder if it's possible to use wp_insert_post in one query.

Thnx and sorry for my bad English.

  • Were is the post data coming from and what format are you using e.g.csv? Commented Nov 9, 2017 at 10:58
  • @Jim-miraidev the post data is coming from text files. I take the content and insert to the DB.
    – StyleSh1t
    Commented Nov 9, 2017 at 11:02
  • can you show me an example of a text file? Commented Nov 9, 2017 at 11:05
  • @Jim-miraidev it's a regular text file which contains regular text. The post title is hard-coded and the post content is the text file content.
    – StyleSh1t
    Commented Nov 9, 2017 at 11:08

2 Answers 2


I would advise using wp_insert_post() and putting you text files into a CSV


Post Title,Post Content,
"title", "content",
"title", "content"...


 $path = "/posts.csv";

        //require __DIR__ . "$path";
        $file = fopen(__DIR__ . $path, 'r');
        while (($line = fgetcsv($file)) !== FALSE) {
            //$line is an array of the csv elements                 
            $post["id"] = wp_insert_post( array(
                "post_title" => $line[0],
                "post_author" => 1,
                "post_content" => $line[1],
                "post_type" => 'page', //add post type
                "post_status" => "publish"

Or you could do a SQL INSERT, have a look at - https://stackoverflow.com/questions/1670838/inserting-a-post-in-wordpress-using-mysql

 $sql = "INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES ";
 $sql .= "(' ','".$post_author."',"."'".$post_date."',"."'".$post_date_gmt."',"."'".$post_content."',"."'".$post_title."',"."'".$post_excerpt."',"."'".$post_status."',"."'".$comment_status."',"."'".$ping_status."',"."'".$posd_password."',"."'".$post_name."',"."'".$to_ping."',"."'".$pinged."',"."'".$post_modified."',"."'".$post_modified_gmt."',"."'".$post_content_filtered."',"."'".$post_parent."',"."'".$guid."',"."'".$menu_order."',"."'".$post_type."',"."'".$post_mime_type."',"."'".$comment_count."'),";
 $res = mysql_query($sql); if($res): print 'Successful Insert'; else: print 'Unable to update table'; endif;

You can't simply replace all things that wp_insert_post does. This function is not running a single SQL query to insert into wp_posts. It is also adding things into wp_postmeta, creates (if needed) and links posts to terms. So, it can run 5 or more queries to create a single post, and all depends on the first one adding into wp_posts because it has to get the post_id from that query to use in other queries for postmeta and terms.

If you need to add posts simply into wp_posts (no need for postmeta or linking to terms during posts creation), you can import CSV data into database via PHPMyAdmin, or you can create one query that includes multiple insert statements to add more then one record. But, from my experience, there is very little gain in sending 10 INSERT queries at once against sending 10 requests each with 1 INSERT query.

  • That's why i would always advise using wp_insert_post() as it does all of the leg work for you. Commented Nov 10, 2017 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.