How to delete all posts and posts meta and then import from csv file
-
Hello there,
I’m trying to make a plugin which reads a file in server and imports the file data into website.
The plugin will first delete each and every listing and its post meta from database and backend and then will read the file and import the posts according to csv file.
Everything was working before but after writing the deletion code IDK whats happened and nothing is working.
The code I’m using is below
<?php /** * Plugin Name: Cron CSV Import * Plugin URI: https://demo.themesuite.com/index.php?plugin=Automotive * Description: Import CSV by file * Version: 1.0.0 * Author: Theme Suite * Author URI: https://www.themesuite.com */ defined('CRON_CSV_IMPORTER_FILE_PATH') || define('CRON_CSV_IMPORTER_FILE_PATH', 'import-csv'); defined('CRON_CSV_IMPORTER_BACKUP_PATH') || define('CRON_CSV_IMPORTER_BACKUP_PATH', 'backups'); function cron_csv_importer_plugin_activate() { $file_dir = trailingslashit( wp_upload_dir()['basedir'] ) . CRON_CSV_IMPORTER_FILE_PATH; $backup_dir = trailingslashit( wp_upload_dir()['basedir'] ) . CRON_CSV_IMPORTER_BACKUP_PATH; wp_mkdir_p( $file_dir ); wp_mkdir_p( $backup_dir ); if ( ! wp_next_scheduled( 'cron_csv_importer_cron_hook' ) ) { wp_schedule_event( time() + 30, 'twicedaily', 'cron_csv_importer_cron_hook' ); } /* activation code here */ } register_activation_hook( __FILE__, 'cron_csv_importer_plugin_activate' ); function cron_csv_importer_run_importer() { require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once( ABSPATH . 'wp-admin/includes/post.php' ); $files = list_files(trailingslashit( wp_upload_dir()['basedir'] ) . CRON_CSV_IMPORTER_FILE_PATH); $backup_dir = trailingslashit( wp_upload_dir()['basedir'] ) . CRON_CSV_IMPORTER_BACKUP_PATH; // if(!empty($files)) return; foreach($files as $file) { if(wp_check_filetype($file)['ext'] !== 'csv') continue; cron_csv_importer_import_file($file); rename($file, $backup_dir. '/'. basename($file, '.csv').'_'.date('d-m-Y'). '_'. time(). '.csv' ); } // mail('[email protected]', 'Cron event completed', 'Yout current cron job just completed'); } function cron_csv_importer_import_file($csvFile) { global $wpdb; $delPosts = $wpdb->get_results("SELECT * FROM
wp_posts
WHEREpost_type
LIKE 'listings'"); foreach( $delPosts as $posts ) { $delpostID = $posts->ID; wp_delete_post($delpostID, true); $delimages = get_post_meta($delpostID, 'gallery_images' , true) ; $unserdelimages = (array)unserialize($delimages); print_r($unserdelimages); wp_delete_attachment( get_post_thumbnail_id($delpostID), true ); $wpdb->get_results("DELETE FROM $wpdb->postmeta WHERE post_id ". $delpostID .""); foreach($unserdelimages as $image) { wp_delete_attachment($image); } } $file = fopen($csvFile, 'r'); $i = 0; $header_array = []; $dealer_id = []; $vin = []; $stock = []; $condition = []; $type_of_vehicle = []; $permalink = []; $year = []; $make = []; $model = []; $model_number = []; $body = []; $transmission = []; $series = []; $body_door_ct = []; $odometer = []; $engine_cylinder_ct = []; $engine_displacement = []; $drivetrain_desc = []; $colour = []; $interior_color = []; $invoice = []; $other_price = []; $book_value = []; $price = []; $inventory_date = []; $certified = []; $description = []; $features = []; $photo_url_list = []; $city_mpg = []; $highway_mpg = []; $photos_last_modified_date = []; $status_code = []; $cost = []; $series_detail = []; $inspection_checklist = []; $secondary_title = []; $engine_description = []; $certification = []; $option_codes = []; $misc_price1 = []; $misc_price2 = []; $misc_price3 = []; $disposition = []; $fuel_type = []; while(! feof($file)) { if($i == 0) { $header_array = cron_csv_import_map_headers(fgetcsv($file)); $i++; continue; } $data = fgetcsv($file); if( !is_array($data) || !array_key_exists($header_array['vin'], $data)) continue; $posts = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'vin-number' AND meta_value = '".$data[$header_array['vin']]."' LIMIT 1", ARRAY_A); if(strtolower($data[$header_array['status_code']]) === 'sold') { if(empty($posts)) continue; $post_id = $posts[0]['post_id']; $images = get_post_meta($post_id, 'gallery_images' , true); wp_delete_attachment( get_post_thumbnail_id($post_id), true ); wp_delete_post($post_id); $wpdb->get_results("DELETE FROM $wpdb->postmeta WHERE post_id = ".$post_id." LIMIT 1", ARRAY_A); foreach($images as $image) { wp_delete_attachment($image); } continue; } if(empty($posts)) { $new_post = array( 'post_title' => wp_strip_all_tags( $data[$header_array['make']]. ' ' . $data[$header_array['model']] ), 'post_name' => wp_strip_all_tags( $data[$header_array['year']] . ' ' . $data[$header_array['make']]. ' ' . $data[$header_array['model']]. ' ' . $data[$header_array['stock']]. ' '), 'post_content' => $data[$header_array['description']], 'post_type' => 'listings', 'post_status' => 'publish', 'post_author' => 1, ); // Insert the post into the database $post_id = wp_insert_post( $new_post ); }else{ $post_id = $posts[0]['post_id']; $post_data = array( 'ID' => $post_id, 'post_title' => wp_strip_all_tags( $data[$header_array['make']]. ' ' . $data[$header_array['model']] ), 'post_name' => wp_strip_all_tags( $data[$header_array['year']] . ' ' . $data[$header_array['make']]. ' ' . $data[$header_array['model']]. ' ' . $data[$header_array['stock']]. ' '), 'post_content' => $data[$header_array['description']], ); wp_update_post( $post_data ); } update_post_meta($post_id, 'vin-number', $data[$header_array['vin']]); $images = $data[$header_array['photo_url_list']]; $image_array = explode('|',$images); $attachment_ids = []; foreach($image_array as $image) { $filename = md5($image).'_'.basename($image); if(post_exists($filename)) { $attachment = get_page_by_title( $filename, OBJECT, 'attachment'); $attachment_ids[] = $attachment->ID; continue; } $uploaddir = wp_upload_dir(); $uploadfile = $uploaddir['path'] . '/' . $filename; $contents= file_get_contents($image); $savefile = fopen($uploadfile, 'w'); fwrite($savefile, $contents); fclose($savefile); $wp_filetype = wp_check_filetype(basename($filename), null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => $filename, 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $uploadfile ); if ($attach_data = wp_generate_attachment_metadata( $attach_id, $uploadfile)) { wp_update_attachment_metadata($attach_id, $attach_data); } $attachment_ids[] = $attach_id; } update_post_meta($post_id, 'gallery_images', $attachment_ids); $listing_options = get_post_meta($post_id, 'listing_options', true); if(empty($listing_options)) { $listing_options = []; }else{ $listing_options = unserialize($listing_options); } $listing_options['price']['value'] = $data[$header_array['price']]; $listing_options['price']['original'] = $data[$header_array['other_price']]; $listing_options['city_mpg']['value'] = $data[$header_array['city_mpg']]; $listing_options['highway_mpg']['value'] = $data[$header_array['highway_mpg']]; update_post_meta($post_id,'listing_options',serialize($listing_options)); $stock[$post_id] = $data[$header_array['stock']]; $dealer_id[$post_id] = $data[$header_array['dealer_id']]; $vin[$post_id] = $data[$header_array['vin']]; $condition[$post_id] = $data[$header_array['condition']]; $type_of_vehicle[$post_id] = $data[$header_array['body_type']]; $permalink[$post_id] = $data[$header_array['permalink']]; $year[$post_id] = $data[$header_array['year']]; $make[$post_id] = $data[$header_array['make']]; $model[$post_id] = $data[$header_array['model']]; $model_number[$post_id] = $data[$header_array['model_number']]; $body[$post_id] = $data[$header_array['body']]; $transmission[$post_id] = $data[$header_array['transmission']]; $series[$post_id] = $data[$header_array['series']]; $body_door_ct[$post_id] = $data[$header_array['body_door_ct']]; $odometer[$post_id] = $data[$header_array['odometer']]; $engine_cylinder_ct[$post_id] = $data[$header_array['engine_cylinder_ct']]; $engine_displacement[$post_id] = $data[$header_array['engine_displacement']]; $drivetrain_desc[$post_id] = $data[$header_array['drivetrain_desc']]; $colour[$post_id] = $data[$header_array['colour']]; $interior_color[$post_id] = $data[$header_array['interior_color']]; $invoice[$post_id] = $data[$header_array['invoice']]; $other_price[$post_id] = $data[$header_array['other_price']]; $book_value[$post_id] = $data[$header_array['book_value']]; $price[$post_id] = $data[$header_array['price']]; $inventory_date[$post_id] = $data[$header_array['inventory_date']]; $certified[$post_id] = $data[$header_array['certified']]; $description[$post_id] = $data[$header_array['description']]; $features[$post_id] = $data[$header_array['features']]; $photo_url_list[$post_id] = $data[$header_array['photo_url_list']]; $city_mpg[$post_id] = $data[$header_array['city_mpg']]; $highway_mpg[$post_id] = $data[$header_array['highway_mpg']]; $photos_last_modified_date[$post_id] = $data[$header_array['photos_last_modified_date']]; $status_code[$post_id] = $data[$header_array['status_code']]; $cost[$post_id] = $data[$header_array['cost']]; $series_detail[$post_id] = $data[$header_array['series_detail']]; $inspection_checklist[$post_id] = $data[$header_array['inspection_checklist']]; $secondary_title[$post_id] = $data[$header_array['secondary_title']]; $engine_description[$post_id] = $data[$header_array['engine_description']]; $certification[$post_id] = $data[$header_array['certification']]; $option_codes[$post_id] = $data[$header_array['option_codes']]; $misc_price1[$post_id] = $data[$header_array['misc_price1']]; $misc_price2[$post_id] = $data[$header_array['misc_price2']]; $misc_price3[$post_id] = $data[$header_array['misc_price3']]; $disposition[$post_id] = $data[$header_array['disposition']]; $fuel_type[$post_id] = $data[$header_array['fuel_type']]; $i++; } } function cron_csv_import_map_headers($data) { return [ "dealer_id" => array_search("DealerId", $data), "vin" => array_search("VIN", $data), "stock" => array_search("Stock #", $data), "condition" => array_search("New/Used", $data), "body_type" => array_search("Body Type", $data), "permalink" => array_search("permalink", $data), "year" => array_search("Year", $data), "make" => array_search("Make", $data), "model" => array_search("Model", $data), "model_number" => array_search("Model Number", $data), "body" => array_search("Body", $data), "transmission" => array_search("Transmission", $data), "series" => array_search("Series", $data), "body_door_ct" => array_search("Body Door Ct", $data), "odometer" => array_search("Odometer", $data), "engine_cylinder_ct" => array_search("Engine Cylinder Ct", $data), "engine_displacement" => array_search("Engine Displacement", $data), "drivetrain_desc" => array_search("Drivetrain Desc", $data), "colour" => array_search("Colour", $data), "interior_color" => array_search("Interior Color", $data), "invoice" => array_search("Invoice", $data), "other_price" => array_search("Other Price", $data), "book_value" => array_search("Book Value", $data), "price" => array_search("Price", $data), "inventory_date" => array_search("Inventory Date", $data), "certified" => array_search("Certified", $data), "description" => array_search("Description", $data), "features" => array_search("Features", $data), "photo_url_list" => array_search("Photo Url List", $data), "city_mpg" => array_search("City MPG", $data), "highway_mpg" => array_search("Highway MPG", $data), "photos_last_modified_date" => array_search("Photos Last Modified Date", $data), "status_code" => array_search("Status Code", $data), "cost" => array_search("Cost", $data), "series_detail" => array_search("Series Detail", $data), "inspection_checklist" => array_search("Inspection Checklist #", $data), "secondary_title" => array_search("Secondary Title", $data), "engine_description" => array_search("Engine Description", $data), "certification" => array_search("Certification", $data), "option_codes" => array_search("Option Codes", $data), "misc_price1" => array_search("MiscPrice1", $data), "misc_price2" => array_search("MiscPrice2", $data), "misc_price3" => array_search("MiscPrice3", $data), "disposition" => array_search("Disposition", $data), "fuel_type" => array_search("Fuel Type", $data), ]; } add_action('init',function(){ if(isset($_GET['run_import'])) cron_csv_importer_run_importer(); }); add_action( 'cron_csv_importer_cron_hook', 'cron_csv_importer_run_importer' ); register_deactivation_hook( __FILE__, 'cron_csv_importer_deactivate' ); function cron_csv_importer_deactivate() { $timestamp = wp_next_scheduled( 'cron_csv_importer_cron_hook' ); wp_unschedule_event( $timestamp, 'cron_csv_importer_cron_hook' ); wp_unschedule_event( $timestamp, 'cron_csv_importer_delete_data' ); }Where I’m making the mistake or what I’m doing wrong due to which it only deletes the listings and post meta but not importing the file accurately.
Thanks
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How to delete all posts and posts meta and then import from csv file’ is closed to new replies.