Photo by Ben Griffiths on Unsplash

Hey there, fellow WordPress enthusiast! Today, I want to share with you a discovery I made while setting up a site on WordPress.

If you’ve ever felt the need to create a bunch of pages or posts at once, you’ll know the monotonous pain of clicking through the dashboard, one page at a time. I felt the same way and thought, “There’s got to be a faster way!”

My first dive into the web came up with a method that honestly, felt a bit like a hack. Some folks suggested editing the functions.php file to insert a PHP script, run the site, and then delete the code right after to prevent duplication. I mean, it works, but it's kind of like using a bulldozer when all you need is a shovel, right?

I wondered if there’s a plugin that would allow me to run custom PHP code without having to delve into the internal files.

I stumbled upon Code Snippets plugin. This amazing tool lets you add custom PHP code to your WordPress site, and — get this — it even has an option to run a script just once! This was the exact tool I was looking for.

Code Snippet plugin lets you manage and run custom PHP, HTML, CSS, and JS code

The run only once function is the best part of it, I don’t want to micro manage the script I only want to run once.

The code is not the interesting part in all of this, though I’ll add a sample script here for reference. Here’s the PHP script I wrote to generate 20 pages with nesting:

// List of pages to create with nested structure
$pages = array(
'Page 1' => array(
'Subpage 1.1',
'Subpage 1.2'
),
'Page 2' => array(
'Subpage 2.1' => array(
'Sub-subpage 2.1.1',
'Sub-subpage 2.1.2'
),
'Subpage 2.2'
),
'Page 3' => array()
);

foreach ($pages as $page_title => $child_pages) {
$page_exists = get_page_by_title($page_title);

if (!$page_exists) {
// Create the parent page
$page_id = wp_insert_post(array(
'post_title' => $page_title,
'post_content' => '',
'post_type' => 'page',
'post_status' => 'publish',
));

// If there are child pages, create them under the parent page
foreach ($child_pages as $child_page_title => $sub_child_pages) {
if (is_array($sub_child_pages)) {
$subpage_id = wp_insert_post(array(
'post_title' => $child_page_title,
'post_content' => '',
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $page_id
));

foreach ($sub_child_pages as $sub_child_page_title) {
wp_insert_post(array(
'post_title' => $sub_child_page_title,
'post_content' => '',
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $subpage_id
));
}
} else {
wp_insert_post(array(
'post_title' => $child_page_title,
'post_content' => '',
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $page_id
));
}
}
}
}

Once I ran this snippet, voila! The pages appeared in my dashboard. And guess what? The same approach works for creating posts too — just switch the ‘post_type’ from ‘page’ to ‘post’.

Here’s the code for creating WordPress posts programmatically:

 // List of sample posts
$posts = array(
'Post 1' => 'Category 1',
'Post 2' => 'Category 2',
'Post 3' => 'Category 1',
);

foreach ($posts as $post_title => $category) {
$post_exists = get_page_by_title($post_title, 'OBJECT', 'post');

if (!$post_exists) {
// Create the post
$post_id = wp_insert_post(array(
'post_title' => $post_title,
'post_content' => 'Sample content for ' . $post_title,
'post_type' => 'post',
'post_status' => 'publish',
'post_category' => array(get_cat_ID($category))
));
}
}

There you have it! A streamlined and efficient way to bulk create pages or posts on WordPress. If you ever find yourself in a situation like mine, don’t waste time doing it manually. Happy coding! 🚀🖥️

This article was first published on Medium. Read the original.