I'm trying to figure out how to create a translation for a post using the internal WPML API (inc/wpml-api.php)

I simply want to create a translation for post ID xx, set some content and publish it.

I've tried to play around with wpml_add_translatable_content but couldn't get it right. Unfortunately there is not much documentation available for this. The closest lead I found is this thread, but I couldn't reduce the code to what I need. It's also possible to do this by directly writing to the database, following WPML's table structure, but I want to use the API.

Any suggestions are welcome.

  • the user of the thread you refer to wants to duplicate images, but that is now already possible with the new Media module. From your question I don't really understand why you would want to go the API way and not work directly with WPML...
    – user2015
    Jun 16, 2011 at 7:59
  • @Piet, on the thread I linked, I'm only interested in the part where he uses wpml_add_translatable_content (he creates translated attachments, which is basically te same a creating translated posts). I want to go with the API because I would like to generate translations from within the code, not the GUI. In the end I'm hoping to be able to use the code inside a plugin I'm working on.
    – mike23
    Jun 16, 2011 at 8:23
  • ok I understand sort of. Have you tried to ask Amir or Mihai in the WPML forum? They usually support plugin developers who want to do the extra work to make their plugins suitable to work with WPML.
    – user2015
    Jun 16, 2011 at 8:30
  • @Piet, yes I've opened a thread on the support forum two weeks ago and we've been discussing. I'm waiting for a conclusive answer but there's no solution yet. I'm sure it'll eventually come, as their support is usually very good. In the meantime I thought I'd ask here, as there are many people using WPML as well as plugin developers.
    – mike23
    Jun 16, 2011 at 8:46
  • yeah WPML support is pretty awesome. Your solution below looks great, thanks for posting it.
    – user2015
    Jun 16, 2011 at 10:59

2 Answers 2


I came up with a function that does the job for now :

 * Creates a translation of a post (to be used with WPML)
 * @param int $post_id The ID of the post to be translated.
 * @param string $post_type The post type of the post to be transaled (ie. 'post', 'page', 'custom type', etc.).
 * @param string $lang The language of the translated post (ie 'fr', 'de', etc.).
 * @return the translated post ID
 *  */
function mwm_wpml_translate_post( $post_id, $post_type, $lang ){

    // Include WPML API
    include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );

    // Define title of translated post
    $post_translated_title = get_post( $post_id )->post_title . ' (' . $lang . ')';

    // Insert translated post
    $post_translated_id = wp_insert_post( array( 'post_title' => $post_translated_title, 'post_type' => $post_type ) );

    // Get trid of original post
    $trid = wpml_get_content_trid( 'post_' . $post_type, $post_id );

    // Get default language
    $default_lang = wpml_get_default_language();

    // Associate original post and translated post
    global $wpdb;
            'trid' => $trid, 
            'language_code' => $lang, 
            'source_language_code' => $default_lang 
            'element_type' => $post_type, 
            'element_id' => $post_translated_id 

    // Return translated post ID
    return $post_translated_id;

  • 2
    This is great but you should also use the 'element_type' column in your update's where clause... Either you may end-up with conflicts ;-) Jan 19, 2012 at 14:34
  • @JonathanLiuti Could you elaborate how to set the column and what would be the right element_type or how to get it? Aug 9, 2014 at 15:47
  • nope sorry, not using wordpress anymore and its far away in my memory ;) Aug 10, 2014 at 10:56
  • @hugoderhungrige I sent an edit request adding Jonathan suggestion. element_id it's not unique in the table, so without checking element_type bad things can happen. Jan 8, 2018 at 18:14

Answer is still good, but if you're having this error Call to undefined function wpml_get_content_trid()

Replace this :

// Get trid of original post
$trid = wpml_get_content_trid( 'post_' . $post_type, $post_id );

With this :

// Get trid of original post
$trid = apply_filters( 'wpml_element_trid', NULL, $post_id, 'post_' . $post_type );

WPML Documentation : https://wpml.org/wpml-hook/wpml_element_trid/

I didn't find any documentation for wpml_get_content_trid() function. I don't really know if this function was deleted in WPML updates or if there is something missing in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.