Scripted mass-creation of WordPress posts

So, the last couple of years I’ve been posting the Advent Calendar set of the day from the LEGO Star Wars and LEGO City box sets. The most mind-numbing part of this was creating the page and tagging it each day, so this year I decided to pre-populate the system with the posts ahead of time.

I leveraged some information on the net and a terrible PHP script, listed below. Whatever you do, I wouldn’t recommend using this on a production system without FIRMLY understanding what’s going on 🙂

In short, I give it the titles of the sets (City, Star Wars) and the year (in this case 2014) and it creates 24 draft posts with the tags required. Since I can automatically get the links, I’ve also created a “rollup” post which I’ll post on xmas day which includes links to all 24 posts.

<?
// last updated 2014-11-23
// created by James Hodgkinson

// designed to programatically create a bunch of posts for my advent calendar posts on my wordpress blog

// http://codex.wordpress.org/Function_Reference/wp_insert_post
// https://tommcfarlin.com/programmatically-create-a-post-in-wordpress/

include( "wp-config.php" );

// post types
$titles = array( "City", "Star Wars" );
$year = 2014;
// Setup the author
$author_id = 1;
foreach( $titles as $title ){
    $rollup = "Here's a list of all the sets in the box this year:<br />\n<ul>";
    for( $i = 1; $i <= 24; $i++ ){
        // Initialize the post ID to -1. This indicates no action has been taken.
        $post_id = -1;
        $slug = "lego-".str_replace( " ", "-", strtolower( $title ) )."-advent-calendar-{$year}-day-{$i}";
        $page_title = "Lego {$title} Advent Calendar {$year} Day {$i}";
        print( "Creating page: '{$page_title}'\n" );

        // If the page doesn't already exist, then create it
        if( null == get_page_by_title( $page_title ) ) {
            // Set the page ID so that we know the page was created successfully

            $post_data =    array(
                'post_content' => '',
                'comment_status'    =>   'open',
                'ping_status'       =>   'open',
                'post_author'       =>   $author_id,
                'post_name'     =>   $slug,
                'post_title'        =>   $page_title,
                'post_status'       =>   'draft',
                'post_type'     =>   'post',
                'tags_input'        => "Advent Calendar, Advent Calendar {$year}, LEGO {$title}",
            );

        $post_id = wp_insert_post( $post_data );
        print( "Created post ID: {$post_id}\n" );
        $rollup .= "<li><a href='".get_permalink( $post_id )."'>{$page_title}</a></li>\n";
        // Otherwise, we'll stop and set a flag
        } else {

            // Arbitrarily use -2 to indicate that the page with the title already exists
            $post_id = -2;

        } // end if
    }
    $rollup .= "</ul>";
    $slug = "lego-".str_replace( " ", "-", strtolower( $title ) )."-advent-calendar-{$year}-rollup";
        $page_title = "Lego {$title} Advent Calendar {$year} Rollup";

        $post_data =    array(
                'post_content' => $rollup,
                'comment_status'        =>      'open',
                'ping_status'           =>      'open',
                'post_author'           =>      $author_id,
                'post_name'             =>      $slug,
                'post_title'            =>      $page_title,
                'post_status'           =>      'draft',
                'post_type'             =>      'post',
                'tags_input'            => "Advent Calendar, Advent Calendar {$year}, LEGO {$title}",
        );
        //`print( var_dump( $post_data ) );
    print( "Creating rollup post for {$title}" );
    $post_id = wp_insert_post( $post_data );
    print ( "\nDone, postID: {$post_id}\n" );
}
echo "\nDone";
?>

 



#Advent Calendar #meta #php #Programming