This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 6 replies, has 2 voices.

Last updated by Itamar 1 month ago.

Assigned support staff: Itamar.

Author Posts
June 1, 2019 at 12:48 am #3933745

The J

I am trying to query posts (WC products) from another MU site, into the current site.

I have already used all the suggested solutions (show only translated posts, $sitepress->switch_lang('lang_code_i_need'), suppress_filters => false, suppress_filters => 'FALSE', do_action( 'wpml_switch_language', 'lang_code_i_need').... the query still retrieves posts in all languages.

The posts retrieved must be in the same language of the site where the query is made.
Both sites have the same default lang (it) and same second lang (en).

Here's a simplified version of the query:

global $current_blog_id;
$current_blog_id = get_current_blog_id();
if(defined('ICL_LANGUAGE_CODE')) {
     $lang = ICL_LANGUAGE_CODE;
} else {
     $lang = 'it';
}
 
$q_args = array(
    'posts_per_page' => 5,
    'post_type' => 'product',
    'post_status' => 'publish',
);
 
$sites = array(2); // MU site ID
 
foreach( $sites as $site ) {
 
    switch_to_blog( $site );
 
    // If site doing the query is different than the site getting posts from
    if(get_current_blog_id() != $current_blog_id) {
        // WPML only show posts in same lang - DOESNT WORK
        $q_args['suppress_filters'] = false; // already tried
        //$q_args['suppress_filters'] = 'FALSE', // already tried        
        /*global $sitepress;
        $sitepress->switch_lang($lang);*/ // already tried
        //do_action( 'wpml_switch_language', $lang); // already tried
    }
 
    // QUERY THE POSTS
    $q = new WP_Query($q_args);
 
    if( $q->have_posts() ) : while( $q->have_posts() ) : $q->the_post();
 
        global $post;
 
        // WPML workaround
        //$translated_slug = apply_filters( 'wpml_get_translated_slug', 'product', 'product', $lang); // apply_filters doesnt work on MU...?
        $post_lang = apply_filters( 'wpml_post_language_details', NULL, get_the_ID());
        if($post_lang["language_code"] != $lang) {
            $skipped++;
            continue;
        }
 
        echo '<a href="' . get_permalink( $post->ID ) . '">Title</a>';
     
    endwhile;    endif;
 
        wp_reset_postdata();    wp_reset_query();
 
        // Debug
    if(get_current_blog_id() != $current_blog_id) {
        echo '<pre>';
        var_dump($skipped); // see how many it skipped = it showed post in the wrong lang
        echo '</pre>';
    }
 
    restore_current_blog();
 
}

No matter what I have tried, nothing worked.
If I use this query on the same site of the site ID, then it works fine (only shows content from the current lang), just not when in other sites.

Also, apply_filter doesnt seem to work (anymore?) on MU, after switching blog, and same with get_permalink, it doesnt get the full url (it skips the post type slug).

Any ideas?

June 2, 2019 at 5:31 pm #3937779

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi.

If I remember correctly from other similar cases, this won't work between sites (not even if it is the same multi-site installation). Anyhow, I am consulting our second tier supporters to hear what do they think about this issue. When I have their reply, I'll comment here.

Thank you for your patience.
Itamar.

June 3, 2019 at 7:01 pm #3945455

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi.

Our second tier supporter asked me to test what happens on a clean multisite installation and escalate if I can replicate. I'll need to set up everything and test. I also might need to ask you some questions on the code. When I have more information, I'll reply here.

Thank you for your patience.
Itamar.

June 4, 2019 at 1:01 am #3946715

The J

Thank you, I'll wait for an answer.
In the meantime, the workaround that you might guess from my code, is that if the blog im querying is different than current one, I simply change the posts_per_page to get more posts... so when I skip them, if post lang doesnt match the lang I need, at least it will keep looping and find enough posts to fill my grid.
It's an ugly hack, but it does the job.

Ever considered making things easier for devs and simply saving lang code as post meta? 🙂
That would solve many, many problems, since it would be easily queryable.

June 4, 2019 at 2:22 pm #3952683

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi.

I've set up the multisite installation on my local server, but I'm not sure how to replicate this issue.

For example, I'm not sure where I should the query that you are using.
If needed please share with me the whole file that this query is included in.
Could you please give me the exact steps on how I should replicate this issue?

Thanks,
Itamar.

June 11, 2019 at 1:00 am #3990873

The J

I am using it as a custom module for Visual Composer, but you dont have to.
In order to replicate, setup a WP MU with 2 sites. On one you have WooCommerce and some products, on the other you dont.
Use the query on the site where you dont have WC and (from a custom template page, for example) query for products from the site where WC is.

Here's the full function with query (adapt it to testable needs) https://pastebin.com/H2k71AVa

June 11, 2019 at 11:43 am #3994777

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi and thanks for the extra details.

I've tried to use your code on my test site according to your instructions. I've added it to the functions.php file of your theme and also as a page template. I've used the basic code that you gave me and also the vc_multisite_widget code. But things didn't work for me, so I've consulted our second tier supporter. He looked at the code and said that maybe it would be better if you provide us with a simple code to use in footer.php.
Can you please do that so we can continue and debug this issue?

Thanks,
Itamar.

The topic ‘[Closed] Custom query in Multisite between 2 sites, doesnt get posts in correct language’ is closed to new replies.