1

how to get the array list of multi-site with multi languages, for e.g i am on site 1 and want to know about other sites languages site 2, site 3.

Site Structure is:

EG (Egypt - with 2 languages)
------------------------
    EN | AR
------------------------
PK (Pakistan - Single language)
    EN
------------------------
LK (Srilanka - Single language)
    EN

i can get the sites with get_sites() but i need to 2 dimensional array containing country code as parent and child contains available languages.

2
  • edit your question to add which plugin you use for languages management
    – mmm
    Commented Oct 14, 2017 at 7:31
  • @mmm i've already added the tag plugin-wpml does it need to add in question too ?
    – Noman
    Commented Oct 14, 2017 at 7:36

1 Answer 1

1

Fix it by querying to wpml languages table.

  1. get all sites
  2. loop through the sites get blog ids.
  3. Query ICL languages table of each blog(each multi-site)
  4. Add into Array & play accordingly

$blog_id == 1 condition used because the base site doesn't have blog id prefix with table name

Here is the function

function getSitesLanguages() {
    $blog_languages = array();
    global $wpdb;
    $sites = get_sites();
    foreach ( $sites as $key => $site ) {
        $blog_languages[ $site->blog_id ] = '';
        $blog_id                           = $site->blog_id . '_';
        if ( $blog_id == 1 ) {
            $blog_id = '';
        }
        $QUERY  = $wpdb->prepare( 'SELECT code FROM ' . $wpdb->base_prefix . $blog_id . 'icl_languages WHERE active =  %d', 1 ).PHP_EOL;
        $result = $wpdb->get_results( $QUERY );
        if ( ! empty( $result ) ) {
            foreach ( $result as $key2 => $langObj ) {
                $blog_languages[ $site->blog_id ][] = $langObj->code;
            }
        }
    }

    return $blog_languages;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.