Support » Plugin: Polylang » get_terms() ignores ‘lang’?

  • Hi,
    I am using a custom taxonomy with few terms, translated into three languages. I only want to get the terms in the current language, so I am doing this:

    
    $options = array(
    	'taxonomy' => 'event_category',
    	'hide_empty' => false
    );
    if (function_exists('pll_current_language')) {
    	$options['lang'] = pll_current_language();
    }
    $terms = get_terms($options);
    

    The $options array then includes ‘lang’ => ‘de’, but I am getting ALL the existing terms anyway. Doesnt matter what I use as the lang parameter, I am getting all the taxonomy terms in the database for the ‘event_category’ taxonomy.

    Is this a bug?

    • This topic was modified 10 months, 1 week ago by  Roman Klabal.
    • This topic was modified 10 months, 1 week ago by  Roman Klabal.
Viewing 9 replies - 1 through 9 (of 9 total)
  • I have ‘Custom Taxonomy Order NE’ plugin installed and as that might change how the query works, I have disabled it – it didnt help with the issue above.

    does you use Polylang or polylang pro ?

    Regular version, not the pro one.

    Isu

    (@isuke01)

    Sorry, I noticed this issue after installing pro, but on other website, I had same error, and my friend to.
    Wierd thing is

    https://polylang.pro/doc/developpers-how-to/

    says it should work. And it worked. Then it stopped.
    It seems to be a some kind of bug.
    I also want point – I use it in WP REST.
    I fixed it using:

    
    $positions_tax = get_terms( 'pos', $args );
    if($positions_tax){
            foreach ($positions_tax as $key => $tax) {
                if(pll_get_term_language($tax->term_id) !== $args['lang']){
                    unset($positions_tax[$key]);
                }
            }
            $positions_tax = array_values($positions_tax);
        }

    Sorry for late answer 😉

    Oh yes, loading all the terms/posts and then filtering them manually works, thanks for the useful snippet!

    But I am very surprised there is no response/fix from the plugin author, this seems like a rather serious and annoying bug to me.

    Has this been fixed?I don’t see it working in polylang ( free version )

    Any soon plans to solve this? same issue here.

    • This reply was modified 2 months, 3 weeks ago by  alibasheer.

    Still an issue today as it seems. I have the issue with MetaBox plugin, all languages are showing when I add

    'query_args' => array(
        'lang' => 'en'
    )

    I don’t want to edit the metabox plugin with the solution from @isuke01 so I have fixed it with the following hook:

    add_filter('get_terms' , 'pll_get_terms_fix', 10, 4);
    function pll_get_terms_fix($terms, $taxonomy, $query_vars, $term_query)
    {
        if(in_array('mytaxonomy',$taxonomy) && function_exists('pll_get_term_language') && isset($query_vars['lang'])){
            foreach ($terms as $key => $term) {
                if($term->taxonomy == 'mytaxonomy' && pll_get_term_language($term->term_id) !== $query_vars['lang']){
                    unset($terms[$key]);
                }
            }
            $terms = array_values($terms);
        }
        return $terms;
    }
    • This reply was modified 5 days, 20 hours ago by  Samuel Diethelm. Reason: typo
    Plugin Author Chouby

    (@chouby)

    Hi,

    I just tested in Polylang 2.3.10

    
    get_terms( array( 'taxonomy' => 'category' ) ); // returns terms in the current language
    get_terms( array( 'taxonomy' => 'category', 'lang' => '' ) ); // returns all languages
    get_terms( array( 'taxonomy' => 'category', 'lang' => 'en' ) ); returns terms in English
    

    They all work as expected.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.