Since the WPML documentation is terrible and all its support forum’s topics are closed prematurely it’s always difficult to get something done with WPML.
As a result of some research, here’s a snippet to help inserting a post (of any post type) in multiple languages programmatically. The resulting posts will be linked to each other just like if you’d create a translation in the graphical interface.
global $sitepress;
$post_type = 'post';
$primary_language = 'en';
$secondary_language = 'nl';
// Insert the original post
$primary_post_id = wp_insert_post( array(
'post_type' => $post_type,
'post_title' => 'Title',
'post_content' => 'Content',
) );
// Insert the translated post
$secondary_post_id = wp_insert_post( array(
'post_type' => $post_type,
'post_title' => 'Titel',
'post_content' => 'Inhoud',
) );
// Get original post's "trid" (translation ID)
$trid = $sitepress->get_element_trid( $primary_post_id, 'post_' . $post_type );
// Tell WPML the second post is a translation of the first
$sitepress->set_element_language_details( $secondary_post_id, 'post_' . $post_type, $trid, $secondary_language );
Thx!
Failed to load resource: the server responded with a status of 500 (Internal Server Error) after import post
And what about taxonomys? attachments? metas? huh?