Frage

I received a php object ($binding_main_cpt) built from an external API like this :

array (
  'post' => 
  array (
    'en' => 
    array (
      'post_title' => 'Villa with magnificent sea view',
      'post_content' => 'Located in ...',
    ),
    'fr' => 
    array (
      'post_title' => 'Villa avec magnifique vue mer',
      'post_content' => 'Située dans un domaine fermé, ...',
    ),
  )
)

I would like to save post with translated versions using WPML (plugin for the premium theme). The default language is "fr".

I managed to insert two posts but WPML does not recognize the link between them. I tried with this solution without success.

My current code

Main code

global $sitepress_settings;
//Insert post with main lang
$post = $binding_main_cpt['post'];
$default_language = $sitepress_settings['default_language'];
$main_post = $post[$default_language];
$postInformation = [
    'post_title' => wp_strip_all_tags(trim($main_post['post_title'] ) ),
    'post_content' => $main_post['post_content'],
    'post_type' => $main_post_type,
    'post_status' => 'publish'
];
$master_post_id = wp_insert_post($postInformation);
echo sprintf( "insert post %d", $master_post_id );
foreach($sitepress_settings['active_languages'] as $lang){
    if( $lang !== $default_language
        && isset( $post[$lang] )
      ){
      $postInformation = [
         'post_title' => wp_strip_all_tags(trim($post[$lang]['post_title'] ) ),
         'post_content' => $post[$lang]['post_content'],
         'post_type' => $main_post_type,
         'post_status' => 'publish'
      ];
      $new_post_id = self::wpml_translate_post( $master_post_id, $main_post_type, $lang, $postInformation );
      echo sprintf( "insert post %d", $new_post_id );
      wp_die();

    }
} 

method used for translated post

private static function wpml_translate_post( int $master_post_id, string $post_type, string $lang, array $arr_post ){

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


    // Insert translated post
    $post_translated_id = wp_insert_post(
        array(
            'post_title'   => $arr_post['post_title'],
            'post_type'    => $post_type ,
            'post_content' => $arr_post['post_content'],
            'post_status'  => $arr_post['post_status']
        )
    );

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

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

    // Associate original post and translated post
    global $wpdb;
    $wpdb->update(
        $wpdb->prefix.'icl_translations',
        array(
            'trid' => $trid,
            'language_code' => $lang,
            'source_language_code' => $default_lang
        ),
        array(
            'element_type' => $post_type,
            'element_id' => $post_translated_id
        )
    );

    // Return translated post ID
    return $post_translated_id;

}

Result on the screen

enter image description here

War es hilfreich?

Lösung

Thank @Sally CJ I made the solution :

main code

global $sitepress_settings;
//Insert post with main lang
$post = $binding_main_cpt['post'];
$default_language = $sitepress_settings['default_language'];
$main_post = $post[$default_language];
$postInformation = [
     'post_title' => wp_strip_all_tags(trim($main_post['post_title'] ) ),
     'post_content' => $main_post['post_content'],
     'post_type' => $main_post_type,
    'post_status' => 'publish'
];
$master_post_id = wp_insert_post($postInformation);
echo sprintf( "insert post %d", $master_post_id );
foreach($sitepress_settings['active_languages'] as $lang){
   if( $lang !== $default_language
       && isset( $post[$lang] )
   {
      $postInformation = [
         'post_title' => wp_strip_all_tags(trim($post[$lang]['post_title'] ) ),
         'post_content' => $post[$lang]['post_content'],
         'post_type' => $main_post_type,
         'post_status' => 'publish'
      ];
      $new_post_id = self::wpml_translate_post( $master_post_id, $main_post_type, $lang, $postInformation );
      echo sprintf( "insert post %d", $new_post_id );
      wp_die();

   }
}

method

private static function wpml_translate_post( int $master_post_id, string $post_type, string $lang, array $arr_post ){
    global $sitepress;
    $def_trid = $sitepress->get_element_trid($master_post_id);

    // Insert translated post
    $post_translated_id = wp_insert_post(
        array(
            'post_title'   => $arr_post['post_title'],
            'post_type'    => $post_type,
            'post_content' => $arr_post['post_content'],
            'post_status'  => $arr_post['post_status']
        )
    );

    //change language and trid of second post to match russian and default post trid
    $sitepress->set_element_language_details( $post_translated_id, 'post_' . $post_type , $def_trid, $lang );

    // Return translated post ID
    return $post_translated_id;

}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top