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?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI 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?
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 IDwp_set_object_terms
- pass it a post ID, a custom taxonomy, and the terms you'd like to setfopen
- for opening a csv filefgetcsv
- reads in a single line from a csv and returns it for processingUse 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
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.
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.