Categories
Uncategorized

How to Inserting multilingual posts programmatically with WPML

How to Inserting multilingual posts programmatically with WPML

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 )

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.