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 13 replies, has 3 voices.

Last updated by thaliaD 6 years, 5 months ago.

Assigned support staff: Bruno.

Author Posts
June 7, 2013 at 5:05 pm #133970

The J

I need to retrieve the original slug or ID (or both if possible) of dynamically found terms.
This is because I associate a term with a specific image, in a list.
Images names are "nameofterm-image.png" and the nameofterm is written in the original website language... I can't create a new image for every translation, I want to use only 1 image for that term, for each translation.

<?php
$terms = get_terms( 'mytaxonomy', array( 'parent' => 0, 'hide_empty' => false ));
foreach ( $terms as $term1 ) {
   $icon = 'get_bloginfo('template_url').'/images/'.$term1->slug.'.png';
}
?>

Now, I have 2 problems:

1) "get_bloginfo('template_url')" will point to the site url with the translation inside too (like mysite.com/en...) and that will not work. I guess I can hardcode the link there, but is there a function to retrieve original url of site, without lang?

2) "$term1->slug" will be the translated slug of the term, not the original one. Well I need the original slug of this term, is there a way to retrieve it? is it stored somewhere in wpml tables?

Thank you

June 7, 2013 at 7:06 pm #134014

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Jany,

Below I added an example of how to get the ID of the original post.

<?php
global $sitepress;

$current_main_id = icl_object_id( get_the_id(), 'post', true, $sitepress->get_default_language() );
$current_slug = get_post( $current_main_id );
$slug = $current_slug->post_name;

echo $current_main_id; //Will display the ID of the original post.
echo $slug; //Will display the slug of the original post.
?>

Please note that the variable $sitepress must be declared globally.

--

1) Use the function home_url() to get the URL without the lang.

2) Use the function get_the_terms() to get the term within post. You can get the post id using the "$current_main_id" cited above.

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Thank you.

June 7, 2013 at 7:14 pm #134016

The J

Also, I have already used the methods that I would normally think of:

<php // I need to know the original slug of this term, from the default lang: it
// Retrieve original term ID: OK
$get_origtermid = icl_object_id($term1->term_id, 'my_taxonomy', false, 'it');
// Method 1: Retrieve term object: FAIL -> object is translated to current lang, EVEN if using original term ID
$term_orig = get_term_by( 'id', $get_origtermid, 'my_taxonomy');
// Method 2: Retrieve term object: FAIL -> object is translated to current lang, EVEN if using original term ID
$term_orig = get_term( $get_origtermid, 'my_taxonomy' );

It looks like there's a filter hooking in the term query, taking the original ID and automatically pre-convert it to the current language term ID, so that the output object is always the current language one, not the original one.

Is it actually a filter? if yes which one and how to remove/unset and reset safely (or things will break later)? Other ideas on how to retrieve the original term object?

June 7, 2013 at 7:19 pm #134017

The J

Bruno, that's useful for posts.
I'm working on a term object, not a post object.
Please see my latest message above this one.

June 7, 2013 at 8:00 pm #134029

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Jany,

Please make a test by adding the term ID directly into the code for us to see the output.

Eg

$term_orig = get_term( 12345, 'my_taxonomy' );

Then see if the output is correct.

Thanks

June 7, 2013 at 10:48 pm #134060

The J

The var_dump($term_orig) simply displays the term object of the translated term for that id, not the original one. So no, the output of $term_orig isnt correct because it displays the translated term object, not the original one.

As I thought, WPML has a filter that interacts with pre_get_posts of the tax_query (get_terms and therefore also get_term_by).
So the ID I find with icl_object_id is correct, but the WPML filter on pre_get_posts automatically converts the term object into the translated one, when using get_terms or similar.

I obviously solved the problem using:
remove_all_filters(get_term);
so now $term_orig contains of course the original term object, and not the translated one.
But this obviously messes with the rest of my function (I ALSO need the translated term object data of course).

So, can you please tell me which is the exact WPML filter that interacts with the tax query, so that I can use remove_filter instead of remove_all_filters and then add the filter again when Im done?
Or, a function that will not be affected by filters (like icl_object_id) that I can use to get original term object?

June 10, 2013 at 1:37 pm #134389

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Jany,

The filter used is "get_term_adjust_id". You can also use the function get_term_adjust_id($term) to try to get the correct ID.

Please let me know if this solves the problem.

Thanks

June 10, 2013 at 4:04 pm #134422

The J

The function get_term_adjust_id() doesnt exist.
Any other function I can use to retrieve original ID of a term name?

Also,

remove_filter('get_term', 'get_term_adjust_id');
remove_filter('pre_get_posts', 'get_term_adjust_id');
remove_filter('wp_get_object_terms', 'get_term_adjust_id');

don't work, so either the filter or the hooks are wrong.

June 10, 2013 at 4:20 pm #134424

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Jany,

Sorry, the method get_term_adjust_id() is inside the file "sitepress.class.php", line 5454. To use it, make sure that the variable $sitepress is global and use:

global $sitepress;
$output = $sitepress->get_term_adjust_id($term);

To remove the filters, please use:

remove_filter ('get_term', array ($sitepress, 'get_term_adjust_id'));

Please let me know if it worked.

Thanks

June 10, 2013 at 4:36 pm #134429

The J

No luck, remove_filter ('get_term', array ($sitepress, 'get_term_adjust_id')); doesnt work, therefore it may be the wrong filter.
The hook (get_term) is actually right, as it works when I use remove_all_filters(get_term)

$term_orig = $sitepress->get_term_adjust_id($term) always outputs the translated name/slug/ID, not the original one (no matter what I put as $term.. whetheter it's the full term1 object, or $term1->term_id, name or slug).

June 20, 2013 at 1:22 pm #137553

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Jany,

This problem was solved? Please let me know if I can help you with anything.

Thanks

June 20, 2013 at 4:32 pm #137627

The J

I had to use remove_all_filters, I dont consider it a solution but I was able to continue development.
However, I suggest your improve your documentation for developers, and list accurately every filter you use (and hooks), so that we can work faster and better.

Thank you anyway.

June 20, 2013 at 4:35 pm #137628

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Jane,

Thanks for the suggestion. I will send to our developers

Thank you

July 9, 2015 at 2:07 pm #656109

thaliaD

Hey, just had to solve the same issue.

In looking at the

get_term_adjust_id

function I saw there is a flag you can toggle to let it know you don't want it to adjust the ids on the next calls.

This function worked for me for WPML version 3.1.9.7

Can easily be modified to work for any language.

function get_term_for_default_lang( $term, $taxonomy ) {
		/** @var $sitepress SitePress */
		global $sitepress;
		global $icl_adjust_id_url_filter_off;

		$term_id = is_int( $term ) ? $term : $term->term_id;

		$default_term_id = (int) icl_object_id( $term_id, $taxonomy, true, $sitepress->get_default_language() );

		$orig_flag_value = $icl_adjust_id_url_filter_off;

		$icl_adjust_id_url_filter_off = true;
		$term = get_term( $default_term_id, $taxonomy );
		$icl_adjust_id_url_filter_off = $orig_flag_value;

		return $term;
}