Skip to content

Instantly share code, notes, and snippets.

@ssuess
Forked from balbuf/wordpress-import-update.php
Last active August 15, 2023 21:25
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ssuess/09efc6431e91a89895ff6b4adf69e643 to your computer and use it in GitHub Desktop.
Save ssuess/09efc6431e91a89895ff6b4adf69e643 to your computer and use it in GitHub Desktop.
Force the WordPress importer to update existing posts instead of skipping them
<?php
/**
* When using the WordPress Importer, update existing
* posts instead of skipping them. Updates content according
* to the import file even if the existing post was updated
* more recently.
*
* To use, drop this file into your /mu-plugins/ folder or
* copy this code into your functions.php file.
*/
class WPImporterUpdate {
protected $existing_post;
function __construct() {
add_filter( 'wp_import_existing_post', [ $this, 'wp_import_existing_post' ], 10, 2 );
add_filter( 'wp_import_post_data_processed', [ $this, 'wp_import_post_data_processed' ], 10, 2 );
}
function wp_import_existing_post( $post_id, $post ) {
if ( $this->existing_post = $post_id ) {
error_log('old post id found: '. $post_id); // log old post id
if ( get_post_type( $post_id ) == 'attachment' ) { // check if this is attachment
error_log("but " . $post_id . " is an attachement, so ignoring"); // log attachement ignoring
$post_id = $post_id; // if attachment ignore, set post_id to real id
} else {
global $wpdb;
$wpdb->delete( 'wp_postmeta', array( 'post_id' => $post_id ) ); //delete existing meta (assumes import contains all meta needed for post)
$post_id = 0; // force the post to be imported
}
} else {
error_log('new post created.'); // log new post created
}
return $post_id;
}
function wp_import_post_data_processed( $postdata, $post ) {
if ( $this->existing_post ) {
// update the existing post
$postdata['ID'] = $this->existing_post;
}
return $postdata;
}
}
new WPImporterUpdate;
@romit19
Copy link

romit19 commented Jan 10, 2019

@ssuess thanks for this code its really helpful 🥇

@w84no1
Copy link

w84no1 commented Feb 18, 2019

@ssuess Thanks for the code, it is what I needed. I do have one issue. When I run the import it changes all paragraph tags to break tags and puts the contents into one paragraph tag.

@spotlighthometours
Copy link

I'm wondering if this will update attached post or media?? I guess I will find out. My situation is unique because I'm using a custom post type... Now that I think of it I believe this will work. Thanks my MLS listing updates I have to run everyday are a million times faster now (I was deleting them then re-importing!)

@guidofd
Copy link

guidofd commented May 24, 2019

@ssuess Thanks for the gist!
Line 27 seems to have a bug, it doesn't do anything.
Also, it wasn't working for me until I realized you hardcoded the $wpdb->prefix on line 30. It should be dynamic.

It was exactly what I needed, though!

@Stevemoretz
Copy link

Comments get duplicated.

@jewelion
Copy link

Many many thanks for this - saved me much work!

@Stevemoretz
Copy link

Many many thanks for this - saved me much work!

Don't think it will solve everything, if you want something accurate you have to make changes still.

@jewelion
Copy link

Many many thanks for this - saved me much work!

Don't think it will solve everything, if you want something accurate you have to make changes still.

For my limited use case, it was perfect.

@karmeljuk
Copy link

Thanks a lot, @ssuess
What is the reason to do this?
$post_id = $post_id;

@jblifestyles
Copy link

jblifestyles commented Apr 14, 2023

This tweak takes care of the duplicate comment issues..

function wp_import_existing_post( $post_id, $post ) {
if ( $this->existing_post = $post_id ) {
// log old post id
error_log('old post id found: '. $post_id);
// check if this is attachment
if ( get_post_type( $post_id ) == 'attachment' ) {
// log attachment ignoring
error_log("but " . $post_id . " is an attachment, so ignoring");
// if attachment ignore, set post_id to real id
$post_id = $post_id;
} else {
// delete existing meta (assumes import contains all meta needed for post)
global $wpdb;
$wpdb->delete( 'wp_postmeta', array( 'post_id' => $post_id ) );
// delete existing comments (assumes import contains all comments needed for post)
$comments = get_comments(array('post_id' => $post_id));
foreach($comments as $comment) {
// format comments
wp_delete_comment($comment->comment_ID, true);
}
// force the post to be imported
$post_id = 0;
}
} else {
// log new post created
error_log('new post created.');
}
return $post_id;
}
function wp_import_post_data_processed( $postdata, $post ) {
if ( $this->existing_post ) {
// update the existing post
$postdata['ID'] = $this->existing_post;
}
return $postdata;
}
}
new WPImporterUpdate;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment