vkal
vkal

Reputation: 51

How do you get wp_terms in current language only?

I am working on a piece of code used to create a woocommerce product in 2 languages and I have managed to do that with the product itself although I am having a problem with the categories for the 2 languages.

$all_cats  = get_terms(array(
    'taxonomy' => 'product_cat',
    'hide_empty' => false,
    'name' => trim($category),
));

using

$sitepress->switch_lang($alt_lang, true);

to switch language and then doing the same query both return the same term_id.

Printing $sitepress->get_current_language() before each query shows the correct language being chosen.

enter image description here

I have also tried both of the first 2 options in case it defaults to one language, I have tried using apply_filters('wpml_object_id',...); and icl_object_id to no avail.

How would I force wordpress to provide me with the categories of the current language only and if there aren't return nothing?

Upvotes: 2

Views: 2186

Answers (1)

montrealist
montrealist

Reputation: 5693

You can pass a language code to the wpml_object_id filter:

$current_language = apply_filters( 'wpml_current_language', NULL );

$translated_cat_ids = array();

foreach ( $all_cats as $term ) {
    $translated_cat_ids[] = apply_filters( 'wpml_object_id', $term->term_id, 'product_cat', false, $current_language );
}

Documentation: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-605256

Upvotes: 2

Related Questions