Truy vấn dữ liệu PluginPolylang
Polylang
Các ví dụ về queries để tương tác với dữ liệu từ plugin Polylang.
Lọc dữ liệu theo ngôn ngữ
Query này lấy các bài viết theo ngôn ngữ đã chọn:
{
posts(
filter: {
polylangLanguagesBy: {
codes: ["en"]
}
}
) {
title
polylangLanguage {
code
}
}
pages(
filter: {
polylangLanguagesBy: {
locales: ["en_US"]
}
}
) {
title
polylangLanguage {
locale
}
}
customPosts(
filter: {
polylangLanguagesBy: {
predefined: DEFAULT
}
customPostTypes: "some-cpt"
}
) {
title
polylangLanguage {
code
}
}
}Query này lấy các thực thể khác nhau cho nhiều ngôn ngữ:
query FilterByLanguage($postId: ID!, $languageCodes: [String!]) {
posts(by: { id: $postId }, filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
title
}
pages(by: { id: $postId }, filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
title
}
customPosts(by: { id: $postId }, filter: {
customPostTypes: ["some-cpt"]
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
title
}
postCategories(by: { id: $postId }, filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
name
}
postTags(by: { id: $postId }, filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
name
}
categories(
by: { id: $postId },
taxonomy: "some-category"
filter: { polylangLanguagesBy: { codes: $languageCodes } }
) {
id
name
}
tags(
by: { id: $postId },
taxonomy: "some-tag"
filter: { polylangLanguagesBy: { codes: $languageCodes } }
) {
id
name
}
mediaItems(by: { id: $postId }, filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
title
}
}Đặt ngôn ngữ cho một thực thể
Query này xác định ngôn ngữ cho 3 bài viết (tiếng Anh, tiếng Tây Ban Nha và tiếng Pháp), sau đó xác định rằng 3 bài viết này là bản dịch của nhau:
mutation {
post1: polylangSetCustomPostLanguage(input: {id: 1, languageBy: { code: "en" }}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
post2: polylangSetCustomPostLanguage(input: {id: 2, languageBy: { code: "es" }}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
post3: polylangSetCustomPostLanguage(input: {id: 3, languageBy: { code: "fr" }}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
polylangSaveCustomPostTranslationAssociation(input: {
ids: [1, 2, 3]
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
}Next