-
Notifications
You must be signed in to change notification settings - Fork 22
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
Get queried/resolved language for a given post #44
Comments
It's not clear what you are trying to achieve from what you have posted. Are you adding a field to post or new root level field? Why do you need There's really no concept of "current language" in the graphql since in single graphql query you can query posts, terms etc. in multiple languages at once so Linking #40 as it might be related. |
Hey @esamattis thanks for a fast reply 😊 In code, this is what I am trying to do:
This way I can query like this:
|
And what Post_Sections_Sections_Faq is? That's not part of wp-graphql or this plugin 🙂 |
Well @esamattis technically it's just Advanced Custom Fields with a Flexible Content section. But in theory, my registering of graph field didn't have to be |
It matters how you get the language. Still guessing what you are doing but maybe something like this? {
post(id: "/en/unlimited", idType: URI) {
something {
... on Post_Sections_Sections_Faq {
questions
}
}
}
} What if query like this is made? {
post(id: "/en/unlimited", idType: URI) {
something {
... on Post_Sections_Sections_Faq {
questions
}
}
}
other: post(id: "/fi/suomeksi", idType: URI) {
something {
... on Post_Sections_Sections_Faq {
questions
}
}
}
} Now your resolver is called twice for posts of different languages. Since In theory it could be possible to patch the global language every time a new post field starts resolving but that can be brittle since it can happen in so many situations. For example posts can be resolved inside posts like with the The "graphql way" to solve this would be to walk up the field tree and check the language of the post your I'm not that familiar how to do it with ACF fields (and you did not even tell what kind of situation you have) but maybe you can ask from the wpgraphql slack. But if your are not using the register_graphql_field('Post', 'questions', [
'type' => 'Json',
'description' => __('Fetches the questions defined in the section'),
'resolve' => function ($post, $args, $context, $info) {
$lang = pll_get_post_language($post->ID);
}
]); |
Even if you need the access to the ACF fields you could use the |
Thanks for your reply.
I am still, very interested how this would be done "the right way", e.g. walking up the tree (from a resolver) and figuring out what language the given post found has. |
Just remembered that WPGraphQL actually calls global $post;
if ($post) {
$lang = pll_get_post_language($post->ID);
} else {
$lang = pll_default_language();
}
I can't remember how it can be done from the ACF group type but do ask from the |
Closing as there's nothing actionable. Feel free to comment still. |
I am trying to register a GraphQL field (https://www.wpgraphql.com/2020/03/11/registering-graphql-fields-with-arguments/), and inside the resolve callback, I need to do a WP query, and for that I’d like to query posts in specific language. How can I get the current language? Unfortunately pll_current_language() returns false.
You specifically say that you have deactivated the "native" way Polylang fetches posts when using WP_Query, but actually, in my case I'd prefer if it only queried in the language that was resolved.
I query by URI like this:
Although not very pretty, I came up with this workaround that gives me the language of the queried object:
A better solution would be if
pll_current_language
could be used insideresolve
ofregister_graphql_field
The text was updated successfully, but these errors were encountered: