Skip to content

Instantly share code, notes, and snippets.

@trey8611
Last active April 19, 2023 03:28
Show Gist options
  • Save trey8611/3a59fa2400db9db52a1b64e6b566124a to your computer and use it in GitHub Desktop.
Save trey8611/3a59fa2400db9db52a1b64e6b566124a to your computer and use it in GitHub Desktop.
[Programmatically update unique identifier] #wpallimport

Before using this snippet, it is really important to back-up your database so that you can roll back if needed. Also:

  • Note the $import_id checks in each function - you need to change '5' to the import ID that you want to target.
  • Note 'id' and {id[1]} in the functions. Change these to the element you want to become the new Unique Identifier.
add_action( 'pmxi_saved_post', 'my_update_uid', 10, 3 );
add_action( 'pmxi_after_xml_import', 'my_update_import_uid', 10, 2 );

function my_update_uid( $post_id, $xml_data, $is_update ) {
	$import_id   = wp_all_import_get_import_id();
	if ( $import_id != 5 ) return;
	
	$import_post = new PMXI_Post_Record();
	$import_post->getBy( array(
		'post_id'   => $post_id,
		'import_id' => $import_id
	) );
	if ( $import_post->isEmpty() ) return;
	
	$elements = json_decode( json_encode( (array) $xml_data ), 1 );
	$uid      = $elements['id'];
	
	$import_post->set( array( 'unique_key' => $uid ) )->update();
}

function my_update_import_uid( $import_id, $import ) {
	if ( $import_id != 5 ) return;
	if ( $import->isEmpty() ) return;
	
	$options = $import->options;
	$options['unique_key'] = '{id[1]}';
	$import->set( array( 'options' => $options ) )->update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment