August 23, 2017
Sometimes, when working with designers, the content of a multilingual website will end up on a single landing page. In this event, using suppress_filters to get_posts associated with the active language will provide posts in both languages, and not provide the side-by-side translation effect desired.
Use this handy snipped to get around the issue:
global $sitepress;
$cur_lang = $sitepress->get_current_language();
$new_lang = 'en'; // change language code you want here.
$sitepress->switch_lang($new_lang);
/*
* Do WP_Query here
*/
$sitepress->switch_lang($cur_lang);
Here it is with a foreach loop:
<?php
global $sitepress;
$cur_lang = $sitepress->get_current_language();
$new_lang = 'fr'; // change language code you want here.
$sitepress->switch_lang($new_lang);
$args = array(
'post_type' => 'member_news',
'posts_per_page' => 3,
'suppress_filters' => 0,
);
$english = get_posts($args);
foreach($english as $post) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>">
<div class="news_content">
<h3><?php the_title(); ?></h3>
<?php the_field('agm_date'); ?>
</div>
</a>
<?php endforeach; wp_reset_postdata();
$sitepress->switch_lang($cur_lang); ?>
Sourced here.