Monday, June 28th, 2010 | downloads, Tips, Web Dev

How to add multiple pages at once with WordPress

I have since written a plugin for this purpose, you may read about it here: WordPress Plugin: Quick Posts

Ok folks, this will be short. If you want to add several pages at once, I created a little script for you.

Here it is:

<?php
 include ('wp-blog-header.php');
 $newPages = array(
 'Page 1' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
 'Page 2' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
 'Page 3' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
 'Page 4' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
 );
 foreach($newPages as $pagename => $content) :

 $postarr = array(
 'post_title'         =>     $pagename,
 'post_content'         =>     $content,
 'post_status'        =>    'publish', // can be changed to 'draft', 'publish', 'pending', or 'future'
 'post_author'        =>    1, // post author 1 is admin, can be changed to the ID of the post author
 'post_type'            =>    'page', // can also use 'post' here.
// 'post_parent'            =>    '22', // (completely optional) you can assign a parent ID to create child pages.
 );
 wp_insert_post($postarr); ?>
 <p>Added <?php echo $pagename?>.</p>
 <?php endforeach; ?>

And here’s how it works:

  1. the include wp_blog_header() is pulling in the file wp_blog_header.php, which gets all the important data from your WordPress blog, allowing you to access the database (very important).
  2. the $newpages variable is storing an array that is created in the next 4 lines.
  3. The foreach cycles through the array, assigning $pagename to the left side of the = and $content to the right, so whatever you put to the left side of the = will be your page title, whatever you put to the right side will be your content (simple enough).
  4. The function wp_insert_post() is called, passing the parameter of the array that was created in step 2.

That’s it, your new pages have been added. Be very careful not to reload the page, or navigate to this page again, or it will duplicate the pages, which can be quite frustrating to straighten out. But that’s it. It really is that easy. You can also see my comments in blue.

I might make a plugin that you can drop into a site to enable users to frame out a site with the pages needed and whatnot. Maybe I’ll make that my next project, anyway, hopefully you found it useful, if you’d like to download the php file, you can get it below:

insertposts.php

Put this file in your root directory (so like: http://geekoutwith.me/insertposts.php). If you put it somewhere else, you’ll have to change the path of the include for wp-blog-header.php

I have since written a plugin for this purpose, you may read about it here: WordPress Plugin: Quick Posts

Filed Under: downloads, Tips, Web Dev

Discussion

  1. August 23, 2010

    Kim says:

    Thanks for this code! Can you advise how I would add a post_parent variable to this array? My intention would be to first add my top level pages then add the subpage groups one by one (so I’d know the parent page’s ID for each group).

  2. August 23, 2010

    Joseph says:

    Kim,

    To assign a post parent (or page parent), simply add post_parent => ID to the array. Any posts you have in the first array ($newPages) would be added as children of that ID.

    I use this method when framing out a site’s architecture. I’ll update the post accordingly, but your second array should look like this:

    $postarr = array(
    ‘post_title’ => $pagename,
    ‘post_content’ => $content,
    ‘post_status’ => ‘publish’, // can be changed to ‘draft’, ‘publish’, ‘pending’, or ‘future’
    ‘post_author’ => 1, // post author 1 is admin, can be changed to the ID of the post author
    ‘post_type’ => ‘page’, // can also use ‘post’ here.
    ‘post_parent’ => 22 // the ID of the page you want to be the parent
    );

  3. January 26, 2011

    James says:

    Hi,

    Very very useful!

    Is it possible to get the new pages to have comments as well? I looked it up and saw a reference to running this:

    wp_update_comment_count_now();

    but couldn’t get it to work, unsure what parameters I’m supposed to parse to it.

  4. January 26, 2011

    Joseph says:

    James,

    I don’t know the answer to that question. I’ve never added comments to a post at once before, only moved them in from another database through MySql.

  5. June 27, 2011

    Eric Kuflik says:

    Hey,

    This has been very helpful. I was trying to add a default page template to this script. Is that possible?

Leave a Comment