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.
<?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; ?>
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:
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
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).
August 23, 2010
Joseph says:
Kim,
To assign a post parent (or page parent), simply add
post_parent => IDto 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
);
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.
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.
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?