-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Have not found a way to fetch Posts or Post by locale
#13
Comments
@rburgst Pinging this too - currently querying |
I know this is an urgent request however, at the moment I am very busy with other things. |
following this thread |
follow |
You can do this: https://DOMAIN.com/graphql?locale=en |
Is it really possible? |
If this can help anyone, i've developped a code that allows you to filter content by WPML locale by adding a new "wpmlLanguage" in the WHERE clause of the following post types:
Keep in mind that i've developped this for my specific use case so it might not be the most optimized. It also hasn't been tested on a website with lots of content, but it works for me right now. This code can be added to a custom plugin or in a theme's functions.php. It has been developped with WP GraphQL 1.6.12 and WPGraphQL WPML 1.0.8.
|
@macharest this looks really good. Would you mind creating a PR so we can integrate this into this plugin? |
@rburgst Sure I could probably integrate this as a PR in the next few days or so. |
@macharest This works well, thank you! In many front end clients (such as apollo or urql) this would require destroying the client instance and re-creating a new client on locale change. It seems like a more elegant solution may be to support reading a request header from the client. Something like "locale: en" for example. Then we can simply invalidate cache on locale change to get fresh queries. Is this something that's possible? |
@revnelson I am not sure I understand, the PR #32 adds a new filter option where you can choose the language via your normal Query. |
@rburgst @macharest is it possible to implement the where also on singular item like Page and Post? |
I am also looking for this solution to return a single translated post eg: I need to retrieve a single post by its slug. In WPGraphQL there is no way to retrieve a single post by ID type slug when using Posts. This only works on singular Post Since where and wpmlLanguage: "es" only works on pages I am unable to retrieve a single post in the specific language Something like this would be ideal
|
@rburgst also running into the same problem as @andyjamesn, being unable to just query a post by its slug and the desired language. I sort of see why, since slugs are meant to be unique, but in the context of wpml where slugs may be the same, it would be incredibly useful. I'm unable to think of a solution that doesn't require two requests (first to get post and the id of its translations, second to get the desired translation). |
I'm using NextJS with SSR, and trying to use pretty permalinks. To identify a unique post, I would like to use its URI or slug. However, using In my use-case, the slugs for different languages will be derived from the translated post's title, so they will (almost certainly) be unique across languages. Having support for querying single posts based on their (translated) slug, would be great. I could extract the language from the language code from the request URI and use it to set a language filter, if a solution like the one @andyjamesn suggested, was implemented. Edit: I don't know how I didn't think of this earlier, but a very easy workaround is to use the multiple object version of the query and using the posts(where: {name: $slug, wpmlLanguage: $languageCode}, first: 1) {
nodes {
id
}
} |
@Ririshi this sounds like a reasonable approach for now, thanks for sharing |
Thank you to anyone who can help with this. Maybe my approach is wrong and you can tell me how you are getting the results I'm looking for.
The text was updated successfully, but these errors were encountered: