As usual – clients have different wishes and requests. Sometimes they’re reasonable, and sometimes they’re not.
This request was not unreasonable. In a country like Sweden, and especially in Stockholm, where many corporations and companies speak english – there are english-only speaking people.
Of course you want your Swedish speaking friends to not miss all of your content. They probably understand English – too ?. And others needs to see how an active blogger you are!
Then you would like the possibility to show blog posts from all languages in your blog. A nice feature is also to show categories with posts from all languages.
I could not find any of these tricks while searching the web or fully described in the Polylang documentation. Maybe I searched with wrong phrases, but then again – someone else probably does too.
How to get posts from all languages in your blog
The lang parameter as described in the documentation needs to be set to empty string, but for some reason the tax_query gets set before the pre_get_posts filter. Nevermind. Just remove the language taxonomy from the query and you will be all set.
function awesome_theme_pre_get_posts( &$query ) {
if ( ! function_exists( 'pll_the_languages' ) ) {
return;
}
if ( $query->is_main_query() and ( is_home() or is_archive( 'post' ) ) ) {
$query->set( 'lang', '' );
if ( is_array( $query->get( 'tax_query' ) ) ) {
$tax_query = $query->get( 'tax_query' );
foreach ( $tax_query as $i => $row ) {
if ( 'language' === $row['taxonomy'] ) {
unset( $tax_query[ $i ] );
}
}
$query->set( 'tax_query', $tax_query );
$query = new WP_Query( $query->query_vars );
}
}
}
add_action( 'pre_get_posts', 'awesome_theme_pre_get_posts', 10 );
How to get categories with posts from all languages
This is much easier. Just set the lang argument to empty string ?
$categories = get_terms( [
'taxonomy' => 'category',
'lang' => '',
] );