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 5 replies, has 2 voices.

Last updated by Radu 4 years, 10 months ago.

Assisted by: Radu.

Author Posts
June 21, 2019 at 3:01 pm #4067905

lucaM-27

Hi,
I'm trying to develop a site using GatsbyJs and WordPress as headless cms.

I used this code in order to get all posts and pages in all available languages:
add_action('rest_api_init', function () {
if (defined('REST_REQUEST') && REST_REQUEST) {
add_action('parse_query', function( $q ) {
$q->query_vars['suppress_filters'] = true;
});
}
});

Is there any way get all media (the same done for posts with the code above)?
Url "/wp-json/wp/v2/media" display only default language medias..

Thank you

June 24, 2019 at 5:20 am #4074607

Radu

Hey Luca,

Thank you for reaching out the WPML support!

Can you tell me if you tried to get the media in all the other languages by adding the language part in the URL like /fr/wp-json/wp/v2/media , for example? This link should work if you selected the Different languages in directories option in Dashboard → WPML → Languages → Language URL format. Can you try this and tell me if it's helping you retrieve the media in a secondary language?

Thanks,
Radu

June 24, 2019 at 7:38 am #4075049

lucaM-27

I think it works, but..
With Gatsby I can't call more than one url (it works with a plugin "gatsby-source-wordpress").

For me it can be also a great solution to have featured_media in default language, instead its translation.

June 25, 2019 at 11:06 am #4083643

Radu

Hey Luca,

I looked over this a bit more and I could see that the WPML REST API is not made to return all the media elements under just one call. There's one small piece of code that might alter the query result. This is it:

add_action('rest_api_init', function () {
if (defined('REST_REQUEST') && REST_REQUEST) {
add_action('parse_query', function( $q ) {
$q->query_vars['suppress_filters'] = true;
});
}
});

You can give it a try and see if there's any difference.

Thanks,
Radu

June 25, 2019 at 2:39 pm #4085555

lucaM-27

Unfortunatly I'm already using this code, as indicated in the first message.

This code works well for posts and pages, but doesn't work with media and other entities, for example tags.

Thanks

June 26, 2019 at 12:27 pm #4092471

Radu

Hey Luca,

Sorry for my mistake. I forgot that you mentioned that piece of code already, in your first message.
I looked a bit more on this but I'm afraid that, at least for now, you can get the media files only like I mentioned, by accessing the route for each language.
You can suggest this as a feature request right here https://wpml.org/suggest-a-new-feature-for-wpml/. This way, my colleagues will have a look and see what can be done about it, in a future update.

Thanks,
Radu

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.