• Hi,

    I’m trying to add a link to a random post on my site. But this post has to be in the language the user is using at the moment. I found this bit of code and it works, except I cannot force it to stay in one language.

    add_action('init','random_add_rewrite');
       function random_add_rewrite() {
           global $wp;
           $wp->add_query_var('random');
           add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
    }
    add_action('template_redirect','random_template');
      function random_template() {
           if (get_query_var('random') == 1) {
                   $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
                   foreach($posts as $post) {
                           $link = get_permalink($post);
                   }
                   wp_redirect($link,307);
                   exit;
           }
    }

    I tried to hardcode the language var here, but it still does not work. And idea where this might go wrong?

    $posts = get_posts('post_type=post&orderby=rand&numberposts=1&lang=fr');

    https://wordpress.org/plugins/polylang/

The topic ‘Link to random post in one specific language’ is closed to new replies.