Created
October 15, 2016 17:54
-
-
Save balbuf/d232769f1e7d66fe91b8ecd7795ef3cb to your computer and use it in GitHub Desktop.
Force the WordPress importer to update existing posts instead of skipping them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
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
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