Skip to main content

WordPress Importer

  • It has support for: deduplication (see below), mapping image urls
  • All imported posts haven import_id meta which is the ID they had in the import WXR.

Post processing:

Example

<?php
 /**
 * ReturnUsage: aIMPORT_POST_TYPES=blogpost,attachment JSONwp arrayimport keyed by guid, with post_id as value. This supports quick lookups....
 */
WP_CLI::add_hook( 'before_invoke:import', function get_guids_for_posttype($post_type='post') : array {
	global $url;
	$command = 'wp --url=' . escapeshellarg( $url ) . ' post list --format=json --post_type=' . escapeshellarg( $post_type ) . ' --fields=ID,guid --porcelain';
	$json = rtrim( shell_exec( $command ), "\n" );
	if ( empty( $json ) ) {
	throw new RuntimeException(add_filter( 'no guids' );
	}

	return json_decode( $json );
}

/**
 * Lookup post by guid.
 *
 * @param string $guid
 *
 * @return WP_Post|null
 */wp_import_posts', function lookup_post_by_guid( string $guid ) : null|WP_Post {
	global $guids;
	global $_cache;
	$guid = str_replace( "&", "&#038;", $guid );
	if ( isset( $_cache[ $guid ] ) ) {
		return $_cache[ $guid ];
	}
	foreach ( $guids as $postposts ) {
		if ( $post->guidgetenv( ==='IMPORT_POST_TYPES' $guid) ) {
			$_cache[ $guid ]post_types = explode( ',', getenv( 'IMPORT_POST_TYPES' ) );
			$post;post_types = array_map( 'trim', $post_types );
			$posts = array_filter( $posts, function ( $post ) use ( $post_types ) {
				return in_array( $post['post_type'], $post_types, true );
			} );

			return $post;
		}posts;
		}

		return null;$posts;
	} );
} );