WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] filtering all language front pages from query (5 posts)

  1. RavanH
    Member
    Posted 1 year ago #

    Hi Chouby,

    Working on my XML Sitemap Feeds plugin again, I need to filter all language front pages from a loop. Currently, I'm doing it like below (creating an array of front page IDs which I then use in the loop to exclude by 'continue')... but I wonder if there is a better method.

    $exclude[] = get_option('page_on_front');
    global $polylang;
    if ( !empty($exclude) && isset($polylang) ) {
    	$langs = array();
    	foreach ($polylang->get_languages_list() as $term)
        		$langs[] = $term->slug;
    
    	foreach ( $langs as $lang )
    		$exclude[] = pll_get_post($exclude[0],$lang);
    }

    After this, I use the array inside the loop:

    if ( in_array($post->ID, $exclude) )
    		continue;

    (please excuse coding errors, this is not the literal code I'm using but a simplified version)

    Any improvement suggestions would be much appreciated :)

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

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    You can work at a lower level to obtain what you want.

    $id = get_option('page_on_front'):
    $exclude = $polylang->get_translations('post', $id);

    You will get an array with the languages slugs as keys and the ids of the translations as values (including $id)

  3. RavanH
    Member
    Posted 1 year ago #

    Excellent! Thanks :)

  4. RavanH
    Member
    Posted 1 year ago #

    Oh... and what does that return when there is no ID (no page on front) given? an empty array or an array of language slug keys with empty values? and what if there are no languages configured yet?

  5. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Currently it should return an empty string, but I will change this to an empty array in the next version (thanks to your question I saw that I am not consistent)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.