WPML Translating a term/taxonomy programmatically

I have all terms in English and I am creating/duplicating the same ones for German language by using foreach on english terms and this way i create all terms for German language.

The problem is when i need to assign an German term to its English version.

I tried the trick with TRID like this:

$trid = $sitepress-get_element_trid($englist_term_id, "taxonomy_name");

$sitepress-set_element_language_details($new_term_id, "taxonomy_name", $trid, $lang_code, $sitepress-get_default_language());

The problem is that $trid does not take any value (The first line of the above code)

Probably there is some other way to accomplish this!

Topic terms plugin-wpml custom-taxonomy Wordpress

Category Web


@agon-xheladini is totally correct with his answer.

But this did not work for me as the WPML API wants to have the term_taxonomy_id instead:

element_id(bool) Use term_taxonomy_id for taxonomies, post_id for posts

So, you should always ensure that you do not use the term_id:

$originalElementId = get_term($original_term_id)->term_taxonomy_id;

$trid = $sitepress->get_element_trid($originalElementId, "tax_" . $taxonomy);
$sitepress->set_element_language_details(
    get_term($new_term_id)->term_taxonomy_id,
    "tax_" . $taxonomy,
    $trid,
    $lang_code,
    $sitepress->get_default_language()
);

Why? term_taxonomy_id and term_id are stored in different WordPress database tables. For usual installations, they have both the same ID for the same term (auto-increment). But I had some installations where the auto increment was different (perhaps a tool "cleared" entries?).


Ok I found the solution.

Actually taxonomy name on icl_translation table is stored with prefix called "tax_" which means taxonomy. The above code would work like this:

$trid = $sitepress->get_element_trid($englist_term_id, "tax_taxonomy_name");

$sitepress->set_element_language_details($new_term_id, "tax_taxonomy_name", $trid, $lang_code, $sitepress->get_default_language());

The difference is tax_ prefix.

Cheers!

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.