2

I have a multilingual Wordpress site with plugin Polylang and the problem is that when I'm seeing site in other language the posts are not showing if there is no translation for the post.

$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 15 ) );
            if ( $latest_blog_posts->have_posts() ) : while (
$latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post(); ?>

I'm using above code to call for the posts, but for now I have posts only in default language (Only posts not pages or categories or any other thing) and everythings OK when I'm seeing site in default language, but when I switch to another one - no posts are shown as there are no translations in other language.

Can anyone help me with this issue. I want to show default language posts if there is no translation.

Sergi Khizanishvili
  • 537
  • 1
  • 7
  • 16

2 Answers2

1

I found the solution and its very simple :) just added 'lang' => '' in array

$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 15, 'lang' => '' ) );
            if ( $latest_blog_posts->have_posts() ) : while (
$latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post(); ?>
Sergi Khizanishvili
  • 537
  • 1
  • 7
  • 16
  • Well, this soultion only gives you posts with all the translations. But it does not select the default language translated post in case there is not translation in the requested language is presented iin the DB. – Valentine Shi Apr 30 '19 at 08:08
0

Polylang has a function to get default language: pll_default_language()

In case: show all posts with default language, try to use this code:

$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 15 ) );
if (!$latest_blog_posts->have_posts()) {
    $latest_blog_posts = new WP_Query( array( 
        'posts_per_page' => 15, 
        'lang' => pll_default_language()
    ));
}
if ( $latest_blog_posts->have_posts() ) : 
   while ($latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post(); ?>
...
Dharman
  • 30,962
  • 25
  • 85
  • 135
namdt55555
  • 409
  • 1
  • 3
  • 14
  • This seems to work if I was interested in building an archive of posts, but what if I want to do this for a single post? Like if I have a post in English but not in French, I just want to show the English post if I click the French flag, instead of being redirected to the home page. How can that be done? – Elliptica May 17 '22 at 04:28