Skip to content

Instantly share code, notes, and snippets.

@tanftw
Last active August 29, 2015 14:25
Show Gist options
  • Save tanftw/fa3ef1df2487a721a6c8 to your computer and use it in GitHub Desktop.
Save tanftw/fa3ef1df2487a721a6c8 to your computer and use it in GitHub Desktop.
Import Solar Companies
<?php
include 'wp-load.php';
set_time_limit(0);
session_start();
if ( ! isset( $_SESSION['import_3'] ) ) :
$file = 'companies.csv';
$headings = array( 'post_title', 'post_content' );
$custom_fields = array( 'address', 'address2', 'area', 'city', 'postcode', 'state', 'country', 'website', 'email', 'phone', 'facebook', 'twitter', 'googleplus', 'pinterest', 'linkedin', 'instragram', 'rss' );
$companies = csv_to_array( $file );
foreach ( $companies as $company )
{
$post = array();
$post['post_type'] = 'company';
$post['post_author'] = 3;
$post['post_name'] = sanitize_title( $company['post_title'] );
$post['post_status'] = 'publish';
foreach ( $headings as $heading )
{
if ( ! empty( $company[$heading] ) )
$post[$heading] = $company[$heading];
}
$post_id = wp_insert_post( $post );
wp_set_object_terms( $post_id, [17468, 17480], 'company_types' );
foreach ( $custom_fields as $custom_field )
{
if ( ! empty( $company[$custom_field] ) )
{
if ( $custom_field == 'website' )
$company[$custom_field] = 'http://' . $company[$custom_field];
update_post_meta( $post_id, $custom_field, $company[$custom_field] );
}
}
//update_post_meta( $post_id, 'user', 17121 ); // Anonymous
}
$_SESSION['import_3'] = true;
echo 'finish!';
else :
p( 'already imported');
endif;
p( 'done!' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment