WPML with WP_Query serving up all 3 languages

All we need is an easy explanation of the problem, so here it is.

I’ve got something about this on the WPML support forums, but I figured I could ask here as well. Here’s the important info: I’m running a WP Multisite setup with custom post types, custom fields, custom everything it seems. In a particular template file, I’ve got a secondary loop that’s pulling a list of items from a different blog. From what I gathered, anything using the WP API should be filtered down to only the current language, but none of the WP_Query() functions I’ve got are doing so.

Relevant code:

$args = array('post_type' => 'release-notes',
    'suppress_filters' => 0,
    'meta_query' => array(
                'key' => 'related_products',
                'value' => $thisproduct,
                'compare' => 'LIKE'
$loop = new WP_Query($args);
if ($loop->have_posts()) {
    echo '<h2>Release Notes</h2>';
    while ( $loop->have_posts() ) : $loop->the_post();
        //echo '<pre>';
        //echo '</pre>';
        $post_link = ($post->post_name);
        echo '<p><a class="text-link" href="' . get_permalink() . '" rel="nofollow noreferrer noopener">' . get_the_title() . '</a></p>';
    endwhile; // foreach($posts
} // if ($results

The problem here is twofold: first, it’s returning results from every language instead of the current language – the same post, three times (EN, FR, DE). Second, the result from get_permalink() is completely wrong – it’s spitting out http://www.mysite.com/blog23name/postname instead of http://www.mysite.com/blog23name/lang/posttype/postname – the custom permalink shown on the ‘edit post’ page.

Any ideas?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

My solution was to register the custom post types on the site where I’m calling it from, then in WPML’s settings set them to translate. This is in WPML -> Translation Management -> Multilingual Content Setup, down at the bottom. I assume it works by tricking WP into using that site’s rewrite/translate rules when it doesn’t have the proper info from switch_to_blog().

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply