Polylang in WordPress Multisite

This tutorial is intended to show you how exactly Polylang plugin works, what database tables it uses and how to get posts filtered by a language if you’re using it within a WordPress Multisite network.

Worth mentioning that at the moment of updating this tutorial Polylang is still my favorite multilingual solution for WordPress.

Polyland Database Tables

What actually happens behind the scenes when you add a new language in Polylang? For example let’s assume we have added a Turkish language like this.

languages menu in Polylang

Each language is a taxonomy. Taxonomy with show_ui=false. When you add languages on different websites of your multisite network, taxonomies will be created separately for each website in wp_{blog id}_terms tables.

Polylang database tables

Each translation is a post that is in a specific language taxonomy.

How to Get Posts in Specific Language

Now we already know that translations are posts, languages are taxonomies. So, we can easily use tax_query parameter in WP_Query or Network_Query (in case you’re using my plugin that helps to query posts from all site in WordPress Multisite network)

WP_Query

$q = new WP_Query(
	array(
		'tax_query' => array(
			array(
				'taxonomy' => 'language',
				'field' => 'slug',
				'terms' => 'tr',
			)
		)
	)
);

Network_Query (WordPress Multisite Network)

Once you rebuild the index, all posts and their translations will go to wp_network_posts, languages will go to wp_network_terms. If these tables are empty and rebuilding the index doesn’t help, just reinstall the plugin.

By default Network_Query (which is part of my plugin) returns all posts in all languages. To get the current language posts you have to pass language term slug in tax_query parameter. In order to get a current language slug, we can use pll_current_language() Polylang function.

$current_lang_slug = pll_current_language(); // current language slug

$multisite_query = new Network_Query( 
	array(
		'tax_query' => array(
			array(
				'taxonomy' => 'language',
				'field' => 'slug', // should be "slug"
				'terms' => $current_lang,
			)
		),
		'post_type' => 'post',
		'post_status' => 'publish'
	)
);

It is also possible to get all terms in specific language from the whole WordPress Multisite network using my plugin. It can be done with the help of network_get_translated_terms() which has been added in 4.8.1 version of the plugin.

$terms = network_get_translated_terms( 'post_tag', array(), array( 'en', 'tr' ) );
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter