This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by enriqueC-3 2 weeks, 4 days ago.

Author Posts
October 2, 2019 at 11:56 am #4679947

enriqueC-3

I would like advice on how to check if a taxonomy term exists in a given language when I am having only the Name for that term.

The case is:
- I have a Name for the term and the language for that name, but I cannot know the slug
- There could be several languages that have the exact same Name for the term (think of a city or region name)
- When I try and use WP term_exists( $term_Name, $taxonomy ) , only 1 term is returned (it seems the first ID that WP can find)
- Therefore I cannot be sure if for a given term Name and Language that terms already exists or not.

What I am trying to achieve is:
- If a term (by Name) exists in a given language
-- Return the ID of that term
- If that term (by Name) does Not exist in a given language
-- Insert that term (using a lang slug, etc) and link it with original language for that term following the standard set_element_language_details and all that stuff.

The only piece I am missing is that I cannot find the way to find terms depending on a known language... The rest I know how to do.

Maybe there could be a workaround to achieve the same...

Thanks!

October 2, 2019 at 2:32 pm #4681501

enriqueC-3

Found a clunky workaround for the moment:

- Look first for the original language terms for given taxonomy
- Then get translation for those terms through wpml_get_element_translations
- Then check all of them to find a match with the given term_NAME and language code that I have
- If exists, here you go, a term with a given Name and language exists in your WP.
- If not, insert a new one or whatever.

It is a bit slow though. I wish there could be a term_exists with a lang parameter to perform directly.

October 7, 2019 at 9:57 am #4706053

kristinaF-2

Could you share your current code? I'm in need of this as well

October 18, 2019 at 11:50 am #4781619

enriqueC-3

Sure,

I ended up with something like this:

$found = term_exists( $term, $tax );
        
        if ( $found !== 0 && $found !== null ) {
            
        
            $term_trid = apply_filters( 'wpml_element_trid', NULL, (int)$found['term_id'], 'tax_category' );
         
            $term_translations = apply_filters( 'wpml_get_element_translations', NULL, $term_trid, 'tax_category' );
         

            foreach ($term_translations as $t_term){
                if ( $t_term->language_code == $lang ){
                    $found_term_id = $t_term->term_id;
                }
            }
            
            //Term NAME EXISTS and also HAS a match for the given lang - return its ID
            if( (isset($found_term_id)) && ($found_term_id !== null) ){
               return (int)$found_term_id;
            }
            //Term NAME EXISTS but NO match for the given lang - Create new Term and Add WMPL relationship with original lang term
            else{
                $new_term = wp_insert_term(
                    $term,
                    $tax,
                    array(
                        'slug' => $term.'-'.$lang,
                    )   
                );
                $new_term_id = $new_term['term_id'];
                
                $set_language_args = array(
                    'element_id'    => $new_term_id,
                    'element_type'  => 'tax_'.$tax,
                    'trid'          => $term_trid,
                    'language_code'   => $lang,
                    'source_language_code' => 'es'  //Original language is Spanish in my scenario
                );
                do_action( 'wpml_set_element_language_details', $set_language_args );
                
                return (int)$new_term_id;
            }
            
        } else{
            //Insert new Term NAME and Add WMPL relationship with original lang term 
            $new_term = wp_insert_term(
                $term,
                $tax
            );
            $new_term_id = $new_term['term_id'];
            
            $original_term_list = wp_get_post_terms( $original_ES_post, $tax, array( 'fields' => 'ids' ) );
            $original_term_trid = apply_filters( 'wpml_element_trid', NULL, $original_term_list[0], 'tax_category' );
            
            $set_language_args = array(
                'element_id'    => $new_term_id,
                'element_type'  => 'tax_'.$tax,
                'trid'          => $original_term_trid,
                'language_code'   => $lang,
                'source_language_code' => 'es'
            );
            do_action( 'wpml_set_element_language_details', $set_language_args );

            return (int)$new_term_id;
        }