Query Plugin Data
Query Plugin DataPolylang

Polylang

Examples of queries to interact with data from the Polylang plugin.

Filtering data by language

This query fetches posts in the selected language:

{
  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
    }
  }
}

This query fetches different entities for multiple languages:

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
  }
}

Setting the language for an entity

This query defines the language for 3 posts (to English, Spanish and French), and then defines that these 3 posts are a translation of each other:

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
      }
    }
  }
}