Skip to content

Instantly share code, notes, and snippets.

@nateluzod
Last active April 13, 2019 19:08
Show Gist options
  • Save nateluzod/39290bf85b087446291c3aa4f57dbe2d to your computer and use it in GitHub Desktop.
Save nateluzod/39290bf85b087446291c3aa4f57dbe2d to your computer and use it in GitHub Desktop.
Create posts from a CSV in WordPress.
<?php
/**
* Run w/in same directory as CSV
*/
$file = fopen( dirname( __FILE__ ) . "/filename.csv", "r" );
while( !feof( $file )){
$line = fgetcsv( $file );
// Print status
echo "Creating " . $line[0] . "... ";
// Skip if the post already exists
if( !get_page_by_title( $line[0], OBJECT, 'post_type' )) {
$post_type = [
'post_title' => $line[0],
'post_type' => 'post_type',
'post_status' => 'publish',
'meta_input' => [
'code' => $line[1],
'participating' => $line[2],
'region' => $line[3]
]
];
// Insert member
wp_insert_post( $post_type );
// Notify on create
echo "Done. <br />";
} else {
// Notify on duplicate
echo "Duplicate, skipping. <br />";
}
}
fclose($file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment