Skip to content

Instantly share code, notes, and snippets.

@freddielore
Last active November 7, 2022 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freddielore/eccfd5da7159356ec698984417a8dc62 to your computer and use it in GitHub Desktop.
Save freddielore/eccfd5da7159356ec698984417a8dc62 to your computer and use it in GitHub Desktop.
Using wp_insert_post to create posts in bulk via CSV import
<?php
add_action( 'smart_seo_import_update', '_child_theme_create_post_with_custom_fields', 10, 3 );
function _child_theme_create_post_with_custom_fields($post_id, $data, $columns){
if( $post_id == 0 || $post_id == '0' ){
$args = array( 'post_title' => $data['post_title'],
'post_status' => 'publish',
'post_type' => $data['post_type'] );
// only create post if no same post is created
$slug = sanitize_title( $data['post_title'] );
$found = get_page_by_path( $slug, OBJECT, $data['post_type'] );
if( is_null( $found ) ){
$id = wp_insert_post( $args, false, false );
if( $id ){
$coords = explode(",", $data['coordinates'] );
update_post_meta( $id, '_address', sanitize_text_field( $data['_address'] ) );
update_post_meta( $id, '_suburb', sanitize_text_field( $data['_suburb'] ) );
update_post_meta( $id, '_developer', sanitize_text_field( $data['_developer'] ) );
update_post_meta( $id, '_contact_name', sanitize_text_field( $data['_contact_name'] ) );
update_post_meta( $id, '_contact_no', sanitize_text_field( $data['_contact_no'] ) );
update_post_meta( $id, '_external_link', sanitize_text_field( $data['_external_link'] ) );
// update_post_meta( $id, '_latitude', $coords[0] );
// update_post_meta( $id, '_longitude', $coords[1] );
update_post_meta( $id, '_latitude', sanitize_text_field( $data['_latitude'] ) );
update_post_meta( $id, '_longitude', sanitize_text_field( $data['_longitude'] ) );
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment