匯入文章更新覆蓋既有內容

多站點內容更新,使用匯入匯出的方式。

1.外掛 Select Post Export

可以選擇post、page 匯出 xml 檔案。

2.匯入 xml 檔案,更新既有內容。

原本的wordpress匯入功能,是不能覆蓋更新既有的post、page 。 要開啟更新功能,在 function.php

加入下列code

<?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;

«
»