Skip Navigation

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 0 voices.

Last updated by davidO-15 19 hours, 8 minutes ago.

Assisted by: Andreas W..

Author Posts
January 8, 2025 at 12:55 pm #16573415

davidO-15

Background of the issue:
I'm having an issue with a function within our theme and WPML translations (connected translated pages). I have this code in our theme:

 function testing_function(){ $data = array(); if(is_multisite()): switch_to_blog(get_main_site_id()); endif; $attachments_custom = get_posts( array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'meta_query' =>  array( array( 'key'     => 'is_icon', 'value'   => '1', 'compare' => '=' ) ) )); if(is_multisite()): restore_current_blog(); endif; } add_action('admin_init', 'testing_function'); 

What seems to happen is that on subsites other than the main blog id, the translation connections don't save and reset.

Symptoms:
On subsites other than the main blog id, the translation connections don't save and reset.

Questions:
Why don't the translation connections save and reset on subsites?
Is there an issue with the function in the theme causing this problem?

January 8, 2025 at 10:05 pm #16575560

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

Take note, that you can not connect original content and translations between different subsites on a multisite network.

Each subsite has its own tables and WPML language settings.

What it the purpose of this function?

Best regards
Andreas

January 9, 2025 at 12:49 pm #16577799

davidO-15

Hi there,

Thanks for coming back to me, this function is a snippet of a core function within our theme. Upon loading WP admin_int, it switches to blog 1, gets some data and switches back to the current blog.

This is causing an issue with WPML when editing subsite pages / translations. They disconnect on save and can't be reconnected.

I have a feel that us either switching blog and using the get_posts or WP Query is messing with WPML doing something at the same time.

For ease, I've spit the function into this snippet of code so that the problem can be recreated on any site.

Many thanks,

Tom

January 10, 2025 at 12:29 am #16580105

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

You are trying to fetch attachments (here icons) from the main site and use them on another subsite of your multisite network?

Disabling this function will solve the issue with WPML?

I need to add again, that WPML can not handle content between different sites of a Multisite network, which might be the reason and this might be expected. I am just asking for confirmation what the main purpose or this approach is.

January 10, 2025 at 8:34 am #16580649

davidO-15

Hi Andreas,

So yes, removing this function (this specific part) resolves the issue with WPML.

The main purpose of the function is to detect custom added icons which have been added on the main site. It all works independently of WPML. The icons "SVG" are detected and then saved into the database "options field" where they are later updated as options within an ACF field.

The problem is that if we run

switch_to_blog(1);
get_posts($args);
restore_current_blog();

Within the WordPress Hook "admin_init"

It seems cause problems with WPML linking translations.

Many thanks,

Tom

January 10, 2025 at 4:55 pm #16582803

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

WPML uses different language settings for each blog, and switching blogs manually can cause WPML's internal language context to be disrupted, leading to inconsistencies in language-specific content.

If you’re using WPML, the situation becomes even more complex because WPML needs to associate translations of attachments with the correct post on each subsite. Transferring attachments might break the translation relationships if not handled properly.

Please try this instead:

// Save the current WPML language
$current_language = apply_filters( 'wpml_current_language', NULL );

// Switch to the blog (1 in this case)
switch_to_blog(1);

// Optionally, make sure you're still on the same language
do_action( 'wpml_switch_language', $current_language );

// Fetch the posts
get_posts($args);

// Restore the current blog
restore_current_blog();

// Optionally, restore the WPML language setting if needed
do_action( 'wpml_switch_language', $current_language );

I have not tested this yet and I can not guarantee if this will solve the issue. Further, please take note that this request is usually out-of-scope for our support. We can only support features that are covered in WPML or open a feature request if possible, but what you are trying to do might simply not be possible with WPML, which is why I can not consider it a bug.

January 14, 2025 at 3:49 pm #16593829

davidO-15

Hi there,

This doesn't appear to have worked. However, I understand that this may not be an issue directly with WPML. We'll review this further on our end to see if this can be resolved.