Skip to content

Instantly share code, notes, and snippets.

@balbuf
Created October 15, 2016 17:54
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save balbuf/d232769f1e7d66fe91b8ecd7795ef3cb to your computer and use it in GitHub Desktop.
Save balbuf/d232769f1e7d66fe91b8ecd7795ef3cb 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 ) {
$post_id = 0; // force the post to be imported
}
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;
@pardocorp
Copy link

great function balbuf just a question, Do you know why the wp:meta_key are not updating?

@brucevdkooij
Copy link

@pardocorp I think it might be because wordpress-importer uses add_post_meta instead of update_post_meta, so it's creating new postmeta entries. (see source code L836).

@balbuf
Copy link
Author

balbuf commented Jun 15, 2018

@brucevdkooij @pardocorp I think that's correct, so assuming the post meta contained in the import file is complete for each post, you'd have to just clear out all existing post meta first (which could be done inside of this if block: https://gist.github.com/balbuf/d232769f1e7d66fe91b8ecd7795ef3cb#file-wordpress-import-update-php-L23-L25). It doesn't look like there is any other way to get the importer to use update_post_meta instead of add_post_meta

@ssuess
Copy link

ssuess commented Sep 1, 2018

Thanks for this code, it is VERY helpful! I reworked it a bit to delete/replace previous meta, and also to ignore if post_type is attachment (because it is very unlikely that they would have been replaced and it takes way longer to load them). If you are interested, my mods are here: https://gist.github.com/ssuess/09efc6431e91a89895ff6b4adf69e643

@vascofmdc
Copy link

Thanks for this. it worked like you said it would.

@ge022
Copy link

ge022 commented Nov 27, 2019

The post's comments were duplicated, not updated. How can I have the comments update instead of duplicate?

@jahir07
Copy link

jahir07 commented Sep 2, 2020

Great. its works custom post type also (Y)

@greenservers
Copy link

two questions...

Does this work for pages as well as posts? Sub-question being if not, any tips on doing that?

And sorry if I overlooked something obvious, but how do I run this? Is it called by using the default /wp-admin/admin.php?import=wordpress

Thanks in advance. Pulling my hair out trying to update pages on import

@wormeyman
Copy link

Thanks this worked great for me. I made this a code snippet to get it to work.

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