Title: Automatically create translation
Last modified: August 22, 2016

---

# Automatically create translation

 *  [alan12341234](https://wordpress.org/support/users/alan12341234/)
 * (@alan12341234)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/automatically-create-translation/)
 * Hi,
 * I’ve been trying to write a function to create translation post automatically
   when the default language post is saved/published. It is because I do not want
   a new translation will erase all custom field values as they are “synced”.
 *     ```
       function publish_article_add_translate( $ID, $post ) {
   
         if (strpos($post->post_title,'auto added')===false) {
           $org_id=$post->ID;
           if (!pll_get_post($org_id,'en')){
             $en_post=clone $post;
             $en_post->ID='';
             $en_post->post_title.=' (EN auto added)';
             $en_post_id=wp_insert_post( $en_post );
             pll_set_post_language($en_post_id, 'en');
           }
           else
             $en_post_id=pll_get_post($org_id,'en');
   
           if (!pll_get_post($org_id,'zh-hans')){
             $hans_post=clone $post;
             $hans_post->ID='';
             $hans_post->post_title.=' (简 auto added)';
             $zh_hans_post_id=wp_insert_post( $hans_post );
             pll_set_post_language($zh_hans_post_id, 'zh-hans');
           }
           else
             $zh_hans_post_id=pll_get_post($org_id,'zh-hans');    
   
           if (!pll_get_post($org_id,'zh-hant')){
             $hant_post=clone $post;
             $hant_post->ID='';
             $hant_post->post_title.=' (繁 auto added)';
             $zh_hant_post_id=wp_insert_post( $hant_post );
             pll_set_post_language($zh_hant_post_id, 'zh-hant');
           }
           else
             $zh_hant_post_id=pll_get_post($org_id,'zh-hant'); 
   
           $translationArray=array('en'=> $en_post_id ,'zh-hans' => $zh_hans_post_id, 'zh-hant' => $zh_hant_post_id);
           if (count($translationArray)==3)
             pll_save_post_translations($translationArray);
   
         }
   
       }
       // add_action( 'publish_fashion-article', 'publish_article_add_translate', 10, 2 );
       ```
   
 * The code only work if I die() the script right after the pll_sve_post_translations()
   function. I can’t figure out why it’s not working, any one has a thought?
 * [https://wordpress.org/plugins/polylang/](https://wordpress.org/plugins/polylang/)

The topic ‘Automatically create translation’ is closed to new replies.

 * ![](https://ps.w.org/polylang/assets/icon-256x256.png?rev=3433336)
 * [Polylang](https://wordpress.org/plugins/polylang/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/polylang/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/polylang/)
 * [Active Topics](https://wordpress.org/support/plugin/polylang/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/polylang/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/polylang/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [alan12341234](https://wordpress.org/support/users/alan12341234/)
 * Last activity: [11 years, 8 months ago](https://wordpress.org/support/topic/automatically-create-translation/)
 * Status: not resolved