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 10 replies, has 3 voices.

Last updated by Nohemi 6 months, 1 week ago.

Assigned support staff: Ahmed Ibrahim.

Author Posts
July 26, 2018 at 3:28 pm #2585151

Nohemi

I am trying to: get some attachments with WP_Query class like this :

$q      = new WP_Query( [
	'post_type' => 'attachment',
	'post__in'  => [ 8071 ],
] );

But strangely the query_vars is modified by WPML like this :

[
	'post_type' => 'attachment',
	'post__in'  => [ 8071 ],
	'post_parent__in' => ["",null]
]

So no attachment is returned. I inspected your code
and i came across the function

adjust_post_parent

from

WPML_Display_As_Translated_Attachments_Query

class.

	public function adjust_post_parent( $q ) {
		if ( isset( $q->query['post_type'] ) && 'attachment' === $q->query['post_type'] && $this->sitepress->is_display_as_translated_post_type( $q->query['post_type'] ) && $this->sitepress->get_current_language() !== $this->sitepress->get_default_language() ) {

			$q->query_vars['post_parent__in'] = array(
				$q->query_vars['post_parent'],
				$this->post_translation->get_original_element( $q->query_vars['post_parent'] )
			);

			unset( $q->query_vars['post_parent'] );
		}

		return $q;
	}

You try to adjust post parent even if there is not 'post_parent' arg in the query vars so obviously it will return null values.

I think you have to check if the arg 'post_parent' exists like this :

if ( isset( $q->query['post_parent'] ) && isset( $q->query['post_type'] ) && 'attachment' === $q->query['post_type'] && $this->sitepress->is_display_as_translated_post_type( $q->query['post_type'] ) && $this->sitepress->get_current_language() !== $this->sitepress->get_default_language() ) 
July 28, 2018 at 2:07 pm #2590832

Vincenzo
Supporter

Languages: English (English ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hello, thank you for contacting WPML Support!

What you describe should happen only if the "display-as-translated" option is enabled for the Media (attachment) post type.
Since WPML 4.0.4 this is not the case anymore.
You can take a look at the wpml-config.xml file in the \plugins\sitepress-multilingual-cms folder to verify it.
Check also the WPML -> Settings -> Post Types Translation to verify that the "use translation if available or fallback to default language" option is not enabled for the Media (Attachment) post type.
In the latest versions of WPML, the Media (Attachment) post type should not be visible in the Post Types list.

Before escalating this issue to our developers, I would like to ask some questions to be able to reproduce this issue.

1. Can you please confirm that you are using the latest versions of WPML and its active add-ons (if using any)?
You can check the latest released versions under “My Account -> Downloads” on this site.
- https://wpml.org/account/downloads/

2. Can you please paste your WPML Debug information in the Debug information field?
- http://wpml.org/faq/provide-debug-information-faster-support/

3. Can you please attach a screenshot of the "Media Translation" section in the WPML -> settings page?
You can use the “upload an image” link below the comment area.

Thank you

Related documentation:
https://wpml.org/documentation/support/language-configuration-files/#custom-types

July 31, 2018 at 7:41 am #2597209

Nohemi

Hello,

I checked "use translation if available or fallback to default language" because @bobby asked me.

See : https://wpml.org/forums/topic/how-to-use-same-media-titles-captions-descriptions-in-all-languages/#post-2490874

I'm on the 4.0.5 version and yes the line is visible.

The wpml-config.xml file (excerpt) :

    <custom-types>
        <custom-type translate="1">attachment</custom-type>
    </custom-types>

I need to check this because otherwise I do not see French media when i'm in German and i can't duplicate posts because i want use the same captions in all languages. (See the post : https://wpml.org/forums/topic/how-to-use-same-media-titles-captions-descriptions-in-all-languages/)

If the code is no longer used, why do not you remove it?

July 31, 2018 at 4:42 pm #2599177

Vincenzo
Supporter

Languages: English (English ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hello,

thank you for the further explanation. I understand now why you enabled that option.

I will escalate this ticket to our 2nd tier. I just need some more information about the issue.

1. Can you please let me know in which language you added the media files that are not working without your modification (eg. Attachment with ID:8071)?

2. Are you able to reproduce the issue if the image is uploaded in the default language?

I'm asking because the "use translation if available or fallback to default language" option is meant to work only for the content in the default language.
You can read more in this article: https://wpml.org/2018/01/wpml-3-9-released/

Please note that this new translation mode works only from the site’s default language to its other languages. You can create content in the site’s default language and it will automatically appear on other languages even if not translated. It doesn’t work the other way around. If you create content in another language (not the site’s default language) this content will not appear on other languages.

Thank you

August 1, 2018 at 12:10 pm #2601793

Nohemi

Hi,

Thank you for your answer.

1. I upload always in the default language but all medias have the problem, even those which are duplicated (yes, because of tests of the other topic, all medias are duplicated except those I've uploaded since.)

2. Yes

Actually, if "use translation if available or fallback to default language" is checked, when i am not on the default language, all medias disappear on frontend.

Thanks for the precision

August 2, 2018 at 5:37 pm #2606501

Vincenzo
Supporter

Languages: English (English ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hi,

thank you for the additional information.

I already forwarded your comments and suggestion to our second tier support.

I will update this ticket as soon as I get an answer.

Thank you for your patience

August 9, 2018 at 5:37 pm #2630273

Ahmed Ibrahim
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+02:00)

Hi, this is Ahmed Ibrahim from the second tier support team.

Thanks for your suggestion, but I was trying to replicate the issue on a clean installation using the following steps but I couldn't:

- Install WPML, ST, TM and media:
- Go to WP media and add an image.
- Set the media to be "display as translated"
- add the following code to the functions.php file with the image ID

function get_image() {
    $the_query      = new WP_Query( [
        'post_type' => 'attachment',
        'post__in'  => [ 98 ],
        'post_status'=>'inherit'
    ] );

    var_dump($the_query->posts);
    exit;
}
add_action( 'wp_head','get_image' );

Then when you visit the front-end on the translated language you will see the image correctly, so is it the same steps that you are using or I'm missing anything?

Best Regards.

August 9, 2018 at 5:52 pm #2630370

Nohemi

Hi Ahmed,

Almost : i don't use media plugin and i unchecked the option "When uploading media to the Media library, make it available in all languages". So there is only one media in the default language (no duplication of medias). After uncheck, if you have already upload a media, you have to delete all its translations or upload a new media.

Edit : maybe the second action (uncheck) is not required.

August 10, 2018 at 12:25 pm #2633936

Ahmed Ibrahim
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+02:00)

Hi,

I have deactivated the media plugin, and unchecked these two options:
- When uploading media to the Media library, make it available in all languages
- Duplicate media attachments for translations

Then uploaded a new image and it was not duplicated, but I still not able to see the issue, so can you please try my steps on a clean installation to see if it will be replicated or send me a duplicator copy from your site, using this plugin: https://wordpress.org/plugins/duplicator/

and upload the package to any online drive and send me the link to download it on the next reply.

Best Regards.

August 13, 2018 at 11:55 pm #2641424

Ahmed Ibrahim
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+02:00)

Hi,

I was not able to replicate because I have added the media directly to the library but as I can see that you added it in a page which gives parent to the image, so thanks for helping me replicate the issue, I have escalated it to our dev team and will update you when I get their reply.

Best Regards.

April 9, 2019 at 3:15 pm #3568667

Nohemi

I upgraded to WPML 4.2.5 and the problem seems to be resolved, thank you.