6

In the theme I develop, I have a template showing 5 posts using the standard loop:

            <?php
          $my_query = new WP_Query('showposts=5');
          while ($my_query->have_posts()) : $my_query->the_post(); ?>

and so on....

There is a page (Actualites) using this template. Now I added polylang and provided another page (News) which uses the same template. There will not be an English translation for the blog entries, but still I would like the english speaking readers to be directed to the News page which will be "the same" as Actualites (in french).

I would assume that the query above produces the same outcome regardless which page is using the template, but in fact the French page works fine, but in News I see no posts. Any ideas?

3 Answers 3

8

OK - I got it, the old posts already on the database do NOT have an english translation and that's the problem with my loop. When I add a new post, I have the option to provide a translation which then appears in the english page.

looking through the docs I found that I can also show the default when there is no translation by something like :

<?php query_posts(array('post_type' => 'post','lang' => 'fr')); // force querying the French posts ?>
if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php global $post;
    if($post_id = pll_get_post($post->ID, pll_current_language())) { // get translated post (in current language) if exists
        $post = get_post($post_id);
        setup_postdata($post);
    }?>

... do what ever you want in your loop ...

1
  • 1
    It looks like this solution is no longer working, is there an alternative?
    – Arturo
    Sep 23, 2021 at 16:37
0

set 'lang' => pll_current_language() in your query for get current lang.

$args = array(
    'posts_per_page' => 5,
    'lang' => pll_current_language(), // Retrieve posts in the current language
);

$my_query = new WP_Query($args);
0

Isn't that easier?

        $paged = get_query_var('page') ?: 1;
        $args = array(
            's'              => get_search_query(),
            'posts_per_page' => 8,
            'paged'          => (int) $paged,
            'fields'         => 'ids',
            'post_type'      => array('any'),
            'tax_query'      => array(
                                    array(
                                    'taxonomy' => 'language',
                                    'field' => 'slug',
                                    'terms' => pll_current_language(),
                                    )
                                ),

        );
        $query = new WP_Query( $args );
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 8 at 14:24

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.