add_filter( 'wpml_import_get_trid_from_imported_translations', integer $trid, object $firstTranslation )
- Type
- filter
- Category
- Inserting Content
- WPML Version
- 4.9.4
Description
This filter is useful when you import translations without their associated source post (which is already on the site). It allows 3rd-party code to retrieve a translation record ID (trid) to reconnect original content with its imported translations.
Use cases:
- Originals exist on the site (not imported), while translations are imported from an external source (spreadsheet or external tool).
- Originals were previously imported and their import meta was cleaned up; new language translations are added later.
- Any workflow where translations must be connected to originals via a shared business key (SKU, slug, external ID, etc.).
Arguments
$trid (int|null)The trid resolved by the importer, or null if none was found.
$firstTranslation (object)
The first translation post object, with properties: element_id, translation_group, language_code, source_language_code.
Example usage
add_filter( 'wpml_import_get_trid_from_imported_translations',
function( $trid, $firstTranslation ) {
if ( $trid ) {
return $trid;
}
$sku = get_post_meta( $firstTranslation->element_id, '_sku', true );
if ( ! $sku ) {
return $trid;
}
// Find the original post with the same SKU and return its trid.
$original_id = wc_get_product_id_by_sku( $sku );
if ( ! $original_id ) {
return $trid;
}
$details = apply_filters( 'wpml_element_language_details', null, [
'element_id' => $original_id,
'element_type' => 'post_product',
] );
return $details ? $details->trid : $trid;
}, 10, 2 );
Related hooks
wpml_set_element_language_details, wpml_make_post_duplicates, wpml_admin_make_post_duplicates, wpml_copy_post_to_language, wpml_delete_package, wpml_register_string, wpml_register_single_string, wpml_show_package_language_ui, wpml_delete_package_action