WordPress.org

Support

Support » Plugins and Hacks » Polylang » [Resolved] Query all language terms

[Resolved] Query all language terms

  • Hi Chouby,

    In the documentation, I read :

    Is it possible to query multiple languages?
    Yes. Example:

    $posts = get_posts(array(
        'post_type' => 'post',
        'lang' => 'de,fr', // query German and French posts
        'showposts' => 5
    ));

    I have two questions (which might have already been asked and answered but the WordPress support forums are impossible to search through) :

    1. Is there a way to include all languages without knowing them?
    Something like the parameter ‘lang’ => ‘all’ (or a *, or empty)… If not, is there maybe a constant that can be set just like for WP Super Cache will be switched off whenever the constant DONOTCACHEPAGE is set to 1 ?

    2. Is there something similar for get_terms() to query all language terms?
    This does not seem to work :

    $terms = get_terms(get_query_var('taxonomy'), array(
        'orderby' => 'count',
        'order' => 'DESC',
        'lang' => 'de,fr', // query German and French posts
        'hierachical' => 0,
        'number' => 50000
    ));

    and pll_get_term() does not cut it since I need all terms including non-translated ones.

    Or maybe there is a way to loop through all site languages and switch to each one dynamically, each time running get_terms() ?

    http://wordpress.org/extend/plugins/polylang/

Viewing 15 replies - 1 through 15 (of 25 total)
  • Plugin Author Chouby

    @chouby

    Hello RavanH!

    You can use:

    global $polylang;
    foreach ($polylang->get_languages_list() as $term)
        $langs[] = $term->slug;
    
    $posts = get_posts(array(
        'post_type' => 'post',
        'lang' => implode(',', $langs),
        'showposts' => 5
    ));

    OK thanks 🙂

    And for get_terms() could that be done in a similar way?

    Plugin Author Chouby

    @chouby

    Sorry, I missed that one. Clearly Polylang does not provide an easy way to do this for the moment. I will come back with a solution.

    Plugin Author Chouby

    @chouby

    Please try the new development version (0.9.5.3). It should now be possible to get terms by language just as you expect in the code in your initial post.
    http://downloads.wordpress.org/plugin/polylang.zip

    That’s fantastic! I’ll be testing it 🙂

    I have a suggestion: would it not have some logic to allow for a wildcard in the ‘lang’ array key value?

    Or rather:
    1. when the lang key is not set, Polylang filters posts (or terms) by the current language
    2. when the lang key is set and has a value (like ‘de,fr’) Polylang filters the languages according to that value
    [ so far nothing new, but then ]
    3. when the lang key is set but has no value (like ”) Polylang does not filter by language(s)

    Similar to some other parameter key / value pairs work in WordPress query functions.

    You see, while adding a ‘lang’ key / value is without risk (because it will simply be ignored when Polylang is not installed) but the foreach loop to check for configured languages will have to be wrapped in an if statement to make sure the $polylang object is set and even then it might break some day (in the far future) if you ever decide to somehow alter how languages are stored in the polylang object affecting how get_languages_list() returns.

    Well, a lot of ifs and I trust you are keen to keep the objects public functions consistent so it’s just a suggestion 🙂

    Hi Chouby,

    I notice that this does not work anymore in later versions than 0.9.5.3 … Was the ‘lang’ array value for get_terms() dropped? I’d really love to have this back again 🙂

    Plugin Author Chouby

    @chouby

    For sure not! But I introduced a bug when refactoring things to support the same for comments. It should work now in the current development version (1.0.1.6)
    http://downloads.wordpress.org/plugin/polylang.zip

    Yep, that works again! Thanks 🙂

    Any chance you could consider my earlier suggestion?

    1. when the lang key is not set, Polylang filters posts (or terms) by the current language
    2. when the lang key is set and has a value (like ‘de,fr’) Polylang filters the languages according to that value
    [ so far nothing new, but then ]
    3. when the lang key is set but has no value (like ”) Polylang does not filter by language(s)

    Similar to how other parameter key / value pairs work in WordPress query functions.

    😉

    There is a problem with the development version:

    When NOT having configured any menus but simply relying on the default WordPress generated one, on a Twenty Eleven site, I notice the menu items are no longer filtered by language…

    Oh, and the admin section language filter (in the top nav) does not seem to work anymore…

    Plugin Author Chouby

    @chouby

    Hi!

    I reproduced the bug with the default menu (in fact, it affects all lists of pages). I will update the development version soon.

    But I don’t have any issue with the admin language filter.

    Regarding your suggestion (3), I will try to look at this in the future (I write that in my todo list), but don’t expect this soon. You know…

    Regarding your suggestion (3), I will try to look at this in the future (I write that in my todo list), but don’t expect this soon. You know…

    I know, and I completely understand. For now, my xml sitemap plugin uses a dedicated polylang get languages routine 🙂

    Regarding the admin language filter, I found that I was clicking the radio buttons and not the text. When you do that, you’ll notice that you can activate all radios at the same time but nothing happens… Only when you click the text, the page refreshes and comes back with the filtered posts. Not a big issue but still, it confused me a bit 😉

    Plugin Author Chouby

    @chouby

    Strange. Can you reproduce in other browsers?

    You’re right, it seems to be only happening in Chrome/Chromium but not Firefox or Opera. Did not test others…

    Plugin Author Chouby

    @chouby

    So I fear I can’t do nothing to help… Basically, I pass html to WordPress functions. Nothing special…

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘[Resolved] Query all language terms’ is closed to new replies.
Skip to toolbar