Best practice for adding posts in bulk

There are many options, but these functions are your friends:

  • wp_insert_post – pass it some arguments and it will create a post and if succesful, return the post ID
  • wp_set_object_terms – pass it a post ID, a custom taxonomy, and the terms you’d like to set
  • fopen – for opening a csv file
  • fgetcsv – reads in a single line from a csv and returns it for processing

Option 1 – Loading a CSV

Use the functions earlier, load up a csv file, then for each line, take the values out, the use those values to call wp_insert_post and wp_set_object_terms

e.g. csv:

oranges,juicy and zesty,fruit
apples,juicy but not so zesty,fruit
cucumber,sandwiches go well with it,vegetable

PHP:

if (($handle = fopen("food.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $title = $data[0];
        $content = $data[1];
        $food_type = $data[2];
        $post_id = wp_insert_post( array(
            'post_title' => $title,
            'post_content' => $content,
            'post_type' => 'toms_food_post_type',
        ));
        wp_set_object_terms( $post_id, $food_type, 'toms_food_taxonomy' );
    }
    fclose($handle);
}

I based the above off of an example from php.net

You may want some logic to skip the first row if you have headings, and some logic to do some error checking, e.g. if your post wasn’t created, or setting the terms failed, etc

Option 2 – WXR

The import file format is based on RSS. So if you can output an RSS document with all your content, and add in the extra fields WordPress uses to signify post type and your custom taxonomy, then you can use the default WordPress importer

As a side note, managing your posts once they’re in WordPress, and the best way to register your post type are completely different questions. If you have 3 questions, create 3 questions rather than bundling them all together. That way you don’t need somebody who has all 3 answers to get something.

Further Reading