How to insert term with same slug programmatically using wpml?

  Kiến thức lập trình

I’m trying to insert a term on wordpress in ita and eng version, so I wrote this function:

protected function insert_update_taxonomy($family_name, $post_id, $lang)
{
  $this->sitepress->switch_lang($lang);

  $collection_id = $this->is_taxonomy_exists($family_name);

  // link taxonomy to post
  wp_set_object_terms($post_id, $collection_id, 'rolex-collection');

  // set default lang
  $this->sitepress->switch_lang($this->sitepress->get_default_language());

  return $collection_id;
}

private function is_taxonomy_exists($family_name)
{
  $slug = sanitize_title($family_name);
  $taxonomy = 'house-collection';
  $args = array(
    'taxonomy'          => $taxonomy,
    'slug'              => $slug,
    'suppress_filters'  => false,
    'lang'              => $this->sitepress->get_current_language(),
  );
  $terms = get_terms($args);

  // existent taxonomy
  if (!empty($terms) && !is_wp_error($terms)) {
    return $terms[0]->term_id;
  } else {
    $current_language = $this->sitepress->get_current_language();
    $result = wp_insert_term($family_name, $taxonomy);
    $term_id = $result['term_id'];
    return $term_id;
  }
}

now the problem’s that when I insert the translated version of the term I get the same term_id of the default language. How’s possible? I added switch_lang which is working fine for insert_post, I don’t undestand why shouldn’t work for wp_insert_term too.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT