0

If we have a term_meta named icon which gets an integer and is set to copy when you translate a term with this meta programmatically it won't get copied to the translated term.

        $ok = wp_insert_term("s", "feature");
        update_term_meta($ok["term_id"], "icon", 54);
        $ok2 = wp_insert_term("س", "feature");
        $taxonomy = "feature";
        $wpml_element_type = apply_filters("wpml_element_type", "feature");
        $originalElementId = get_term($ok["term_id"])->term_taxonomy_id;

        global $sitepress;
        $trid = $sitepress->get_element_trid(
            $originalElementId,
            "tax_" . $taxonomy
        );
        $sitepress->set_element_language_details(
            get_term($ok2["term_id"])->term_taxonomy_id,
            "tax_" . $taxonomy,
            $trid,
            "fa",
            $sitepress->get_default_language()
        );

        $ttid = apply_filters(
            "wpml_get_element_translations_filter",
            "",
            $trid,
            $wpml_element_type
        )["fa"]->term_id;

//        wp_update_term($ok2["term_id"], $taxonomy, []);
        dd(
            get_term($ttid),
            get_term_meta($ttid),
            get_term_meta($ok["term_id"])
        );

the output is:

WP_Term]8;;file:///Volumes/HDD/Websites/central-graphql/wp-includes/class-wp-term.php#L18\^]8;;\ {#7032
  +term_id: 196
  +name: "س"
  +slug: "%d8%b3"
  +term_group: 0
  +term_taxonomy_id: 174
  +taxonomy: "feature"
  +description: ""
  +parent: 0
  +count: 0
  +filter: "raw"
}
[]
array:1 [
  "icon" => array:1 [
    0 => "54"
  ]
]

I used this in phpunit, it shows the term meta is not copied.

1 Answer 1

1

The trick is the update the original term.

wp_update_term($term_id, $taxonomy, []);

This will trigger the WPML actions that sync the meta, notice we provided [] so technically we are not updating anything but this is good enough to trigger the sync.

1
  • Also don't forget to clear the term_meta cache for that translated term after the term_update. Commented Dec 31, 2022 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.