2

I have a set of custom post types and taxonomies I've added using the Custom Post Type UI plugin, and need to enter several hundred entries ( >500 )

How can I import this data in bulk? E.g. as a csv?

  • 2
    It is very tempting to edit this question down to these 9 words: "How do I bulk create posts from a csv?" – Tom J Nowell Mar 26 '15 at 20:23
  • @TomJNowell Updated. Hopefully that's more clear. Thanks – ServAce85 Mar 26 '15 at 21:07
2

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

| improve this answer | |
-1

You should really re-title this question and refine your question further. From what I gather you are actually trying to find out how to add posts to your custom post type that has custom fields via a CSV.

My suggestion is to look for a plugin that imports posts from a CSV and either find one that already has a column for post_type and columns for custom fields for extend the plugin to include that.

This plugin should do exactly the trick for you https://github.com/dansullyLT/rs-csv-importer

I can't give any specific coding / programming advice seeing as this is an extremely broad question and not a question on a specific function.

| improve this answer | |
  • Thanks - I've updated the title accordingly. I'll check out that plugin you mentioned. That said, I intentionally left the question vague to learn the best practice for this type of problem. I want to learn how people typically go from a clean Wordpress install to a site with hundreds/thousands of custom posts (i.e. products, etc.). I'm not really sure that the process that I outlined is the best method. – ServAce85 Mar 26 '15 at 20:55
  • 1
    We prefer precise/specific questions rather than vague ones, it's a Q&A site not a discussion forum, you need to be able to mark an answer as correct. If you need to ask multiple questions, you can create multiple questions – Tom J Nowell Mar 26 '15 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.