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.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 -
- 12:00 – 16:00 12:00 – 16:00 12:00 – 16:00 12:00 – 16:00 12:00 – 16:00 -

Supporter timezone: Europe/Zagreb (GMT+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Dražen 1 year ago.

Assisted by: Dražen.

Author Posts
February 16, 2024 at 7:04 pm #15312950

jamesH-8

Is there a way to get translated pages by nodeByUri?

I'm using Astro JS and in my language folder have a [...slug].astro file which has the following code which essentially should look for all page in French and then separately get the page by its URI and pass all the relevant data. However, it seems you cant use nodeByUri to filter through translated pages so it executes an error :

Can you think of a work around?

---
import type { Block } from '@wp-block-tools/styles';
import BlockRenderer from '@components/BlockRenderer.astro';
import CommonHead from '@components/CommonHead.astro';

export async function getStaticPaths(){
const response = await fetch(`${import.meta.env.WPGRAPHQL_URL}`, {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
query: `
query AllPages {
pages(where: {language: "fr"}, first: 10000) {
nodes {
uri
}
}
}
`,
}),
});

const { data } = await response.json();

return data.pages?.nodes.map((page: any) => ({
params: {slug: page.uri === "/" ? undefined : page.uri},
}));
}

const response = await fetch(`${import.meta.env.WPGRAPHQL_URL}`, {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
query: `
query PageQuery($uri: String) {
nodeByUri(uri: $uri) {
... on Page{
uri
blocks
seo {
metaDesc
title
}
}
}
}
`,
variables: {
uri: Astro.params.slug,
},
}),
});
const { data } = await response.json();
const blocks = (data.nodeByUri?.blocks || [] ) as Block[];
const seo = data.nodeByUri?.seo;
---

<html lang="en">
<CommonHead title={seo.title || ""} description={seo.metaDesc || ""}/>
<body>
<BlockRenderer blocks={blocks}/>
</body>
</html>

February 19, 2024 at 9:50 am #15316921

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

thanks for contacting us.

I have checked the related code but not sure if you are using any extra code or addon that may be causing this. Please note we can not help with debugging custom-coded solutions, for this, we recommend WPML Contractors (https://wpml.org/contractors/).

We will do the best we can to help out and advise you.

1) Can you please confirm you have checked our related docs and using the required addon https://wpml.org/documentation/related-projects/wpml-graphql/?

https://wpml.org/documentation/related-projects/wpml-graphql/

2) Does the issue happen with simple examples and by following our docs, using only WP default theme and WPML? I am asking to see if this can be reproduced in simple test site from scratch.

3) I see a similar report on the next link, please check if it is helpful:

- hidden link

Regards,
Drazen

The topic ‘[Closed] Calling translations using nodeByUri in graphQL?’ is closed to new replies.