2

I'm working on a property feed importer for a website I'm having some trouble getting the Taxonomy to synchronize correctly.

This is the code to add the translations

  foreach($languages as $lang => $data){
    $translation = 0;
    $trans_arr = array(
      'post_author'=>$author,
      'post_content'=>$property->get_description($lang),
      'post_title'=>$property->get_title($lang),
      'post_status'=>'importing', //Don't show the property while its still importing
      'post_type'=>'property',
      'comment_status'=>'closed',
      'ping_status'=>'closed',
      'post_date'=>$property->date, //Use the property date to keep track of changes
      'meta_input'=>array("kyero_data"=>json_encode(array('feed' => $feed_token,"kyeor_id" => $property->ref, "lang"=>$lang ))),
    );
    
    // Check if the translation already exists so we update it instead
    $_translation = icl_object_id( $post, 'property', false, $lang );
    if($_translation){
      $trans_arr['ID'] = $_translation;
    }
    
    // Create/update the translation
    $translation = wp_insert_post($trans_arr);

    // If successfull set the correct language information of the post.
    if(is_int($translation) && $translation !== 0){
      wp_set_post_terms( $translation, $tax['property_feature'], 'property_feature', false );
      wp_set_post_terms( $translation, $tax['property_status'], 'property_status', false );
      $sitepress->set_element_language_details($translation, 'post_property', $def_trid, $lang);
      set_post_thumbnail($translation, $feat);
      wp_publish_post($translation);
    }
  }

$languages is an array with all the languages (keys are the language codes) except for the default language.

$tax['property_feature'] and $tax['property_status'] contain a list of IDs are filled by the feed and are only available in the default language inside the feed (ID are fetched by get_term_by using the name) however they are translated in the website.

Please can some one help me out, is there a way to synchronize the the taxonomy of a single WP_Post object with its parent programmatically


I found a solution and taught I mention it here in case some one else may need it at some point.

Instead of using the wpml class oject to set the language $sitepress->set_element_language_details using the hook wpml_set_element_language_details

  foreach($languages as $lang => $data){
    $translation = 0;
    $trans_arr = array(
      'post_author'=>$author,
      'post_content'=>$property->get_description($lang),
      'post_title'=>$property->get_title($lang),
      'post_status'=>'importing', //Don't show the property while its still importing
      'post_type'=>'property',
      'comment_status'=>'closed',
      'ping_status'=>'closed',
      'post_date'=>$property->date, //Use the property date to keep track of changes
      'meta_input'=>array("kyero_data"=>json_encode(array('feed' => $feed_token,"kyeor_id" => $property->ref, "lang"=>$lang ))),
    );
    
    // Check if the translation already exists so we update it instead
    $_translation = icl_object_id( $post, 'property', false, $lang );
    if($_translation){
      $trans_arr['ID'] = $_translation;
    }
    
    // Create/update the translation
    $translation = wp_insert_post($trans_arr);

    // If successfull set the correct language information of the post.
    if(is_int($translation) && $translation !== 0){
      do_action('wpml_set_element_language_details',array('element_id'=>$translation, 'element_type'=>'post_property', 'trid'=>$def_trid, 'language_code'=>$lang, 'source_language_code'=>$default_lang));
      set_post_thumbnail($translation, $feat);
      wp_publish_post($translation);
    }
  }
Jasper B
  • 851
  • 4
  • 13
  • Thanks for sharing. Where is `$def_trid` coming from? – montrealist Apr 22 '21 at 21:46
  • after creating the post I store its ID in a variable `$post = wp_insert_post($post_arr);` and then I use the sitepress element to get its trid `$def_trid = $sitepress->get_element_trid($post);` make sure to include `global $sitepress;` in your function so you have access to the sitepress object – Jasper B Apr 23 '21 at 14:02
  • Hi. If you found a solution, and it is not in the answers, then feel free to answer your own question. – no ai please Aug 02 '22 at 17:22

0 Answers0