[Plugin: CSV Importer] Overwrite posts
-
Hi! Is there a way to use this plugin to overwrite posts? I need to mass update taxonomies and descriptions from existing posts but, if I use this plugin, it duplicates them
example
myweb.com/post-slug
when I import another post with this slug, it generates:
myweb.com/post-slug-2
-
Yes, see this other thread for more information.
That thread is closed now, and I never put the details of the minor code changes needed.
Starting with version 0.3.7 as a base, make 3 changes in csv_importer.php as follows:
Before Line 277 add:if (!empty($new_post['ID'])) $id = wp_update_post($new_post); else
at Line 254 add:
'ID' => convert_chars(trim($data['csv_post_id'])),
at Line 40 add:
'csv_post_id' => null,
Do it in that order, so the line numbers are right.
You can get a nice export file to work with using phpMyAdmin or try this exporter.
Hi,
I solved this issue in my project,
Solution:
Just changed the create_post function in the csv-importer.php file,function create_post($data, $options) { extract($options); <strong>global $wpdb;</strong> //edited by guru $data = array_merge($this->defaults, $data); $type = $data['csv_post_type'] ? $data['csv_post_type'] : 'post'; $valid_type = (function_exists('post_type_exists') && post_type_exists($type)) || in_array($type, array('post', 'page')); if (!$valid_type) { $this->log['error']["type-{$type}"] = sprintf( 'Unknown post type "%s".', $type); } $new_post = array( 'post_title' => convert_chars($data['csv_post_title']), 'post_content' => wpautop(convert_chars($data['csv_post_post'])), 'post_status' => $opt_draft, 'post_type' => $type, 'post_date' => $this->parse_date($data['csv_post_date']), 'post_excerpt' => convert_chars($data['csv_post_excerpt']), 'post_name' => $data['csv_post_slug'], 'post_author' => $this->get_auth_id($data['csv_post_author']), 'tax_input' => $this->get_taxonomies($data), 'post_parent' => $data['csv_post_parent'], ); <strong>// edited by guru $new_post['ID'] = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $data['csv_post_title'] . "'" ); // ends</strong> // pages don't have tags or categories if ('page' !== $type) { $new_post['tags_input'] = $data['csv_post_tags']; // Setup categories before inserting - this should make insertion // faster, but I don't exactly remember why :) Most likely because // we don't assign default cat to post when csv_post_categories // is not empty. $cats = $this->create_or_get_categories($data, $opt_cat); $new_post['post_category'] = $cats['post']; } <strong>// create or update edited by guru! if(!empty($new_post['ID'])) { wp_update_post($new_post); } else { wp_insert_post($new_post); } // ends</strong> if ('page' !== $type && !$id) { // cleanup new categories on failure foreach ($cats['cleanup'] as $c) { wp_delete_term($c, 'category'); } } return $id; }
[Moderator Note: Please post code or markup snippets between backticks or use the code button. Or better still – use the pastebin. As it stands, your code may now have been permanently damaged/corrupted by the forum’s parser.]
just copy the above function create_post and replace the one in the csv-importer.php at line no 239
@pulsarcoder, your code is missing the assignment of the $id for the new post. No telling what else is wrong.
Mine has been tested to work both with and without replacing the posts.Hi
Thanks for your reply, please use the following code,
$id is the return value of wp_update_post or wp_insert_post.Actually in my project, i have so many posts also i have to update the post id in the excel as csv_post_id. instead i just changed the code to take the post title and find the id from the db.
If exists the post is overwritten else inserted as new post.please kindly post your feedback.
function create_post($data, $options) { extract($options); <strong>global $wpdb;</strong> //edited by guru $data = array_merge($this->defaults, $data); $type = $data['csv_post_type'] ? $data['csv_post_type'] : 'post'; $valid_type = (function_exists('post_type_exists') && post_type_exists($type)) || in_array($type, array('post', 'page')); if (!$valid_type) { $this->log['error']["type-{$type}"] = sprintf( 'Unknown post type "%s".', $type); } $new_post = array( 'post_title' => convert_chars($data['csv_post_title']), 'post_content' => wpautop(convert_chars($data['csv_post_post'])), 'post_status' => $opt_draft, 'post_type' => $type, 'post_date' => $this->parse_date($data['csv_post_date']), 'post_excerpt' => convert_chars($data['csv_post_excerpt']), 'post_name' => $data['csv_post_slug'], 'post_author' => $this->get_auth_id($data['csv_post_author']), 'tax_input' => $this->get_taxonomies($data), 'post_parent' => $data['csv_post_parent'], ); <strong>// edited by guru $new_post['ID'] = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $data['csv_post_title'] . "'" ); // ends</strong> // pages don't have tags or categories if ('page' !== $type) { $new_post['tags_input'] = $data['csv_post_tags']; // Setup categories before inserting - this should make insertion // faster, but I don't exactly remember why :) Most likely because // we don't assign default cat to post when csv_post_categories // is not empty. $cats = $this->create_or_get_categories($data, $opt_cat); $new_post['post_category'] = $cats['post']; } <strong>// create or update edited by guru! if(!empty($new_post['ID'])) { $id = wp_update_post($new_post); } else { $id = wp_insert_post($new_post); } // ends</strong> if ('page' !== $type && !$id) { // cleanup new categories on failure foreach ($cats['cleanup'] as $c) { wp_delete_term($c, 'category'); } } return $id; }
This looks great! I’ll add this to my fork of the plugin (pretty soon, I’ll release it as a separate plugin).
Hi Joy, thanks for the perfect instructions on adding those lines of code. Works great on overwriting a post.
One question for you. How could I ensure that the custom fields also get overwritten? When I use your code it adds a while new custom field with the updated values.
Any suggestions would be greatly appreciated.
Cheers.
Jason
Ah surfrunner, you are correct that the custom fields were not taken into account for updates. Neither were comments.
But WordPress has it covered.Search the code for add_post_meta and change it to update_post_meta. That function will call add if it doesn’t exist. (this could be a problem if you have multiple custom fields with the same name, such as image attachments…I haven’t tried it)
For comments, search for wp_insert_comment and change it to wp_update_comment. There might be more filtering going on than before, using that.
Thanks Joy.
That update worked, the custom fields are overwritten versus copied (very cool).
It does not work when I try to use it with pages that have 12+ custom fields… I get an error on import claiming some fields may be empty.
Kind of weird that it works if I import just the basics but when the .csv gets a bit more complicated it will not import (not because there are multiple custom fields with the same name though, I removed those and still get the error).
But thanks! I’ll keep tweaking and get it to work somehow. I appreciate your quick response.
- The topic ‘[Plugin: CSV Importer] Overwrite posts’ is closed to new replies.