Queries Library
Queries LibrarySync the post's featured image for Polylang

Sync the post's featured image for Polylang

This GraphQL query is an integration for the Polylang plugin (and also Polylang PRO).

This query requires the endpoint to have Nested Mutations enabled.

It takes an origin post, queries its featured image, and updates its translation posts with the corresponding featured image for the corresponding language, as set-up via Polylang. If there is no translation for that image, the same image from the origin post is set.

If the origin post does not have a featured image, it removes it from all translation posts.

All translation posts must have the draft status to be updated. To update posts with any other status, use variable $statusToUpdate (for instance, passing value publish, pending or any).

By default, the origin post must have the default language of the site, or the update will not be executed. To trigger the update from any language, pass variable $triggerUpdateFromDefaultLanguageOnly with false.

########################################################################
# 
# Variables:
#   - postId: The post with the origin language, from where to trigger the featured image updates
#   - statusToUpdate: The status that the translation posts must have to be updated. It is `draft` by default. Pass `any` for any status.
#   - triggerUpdateFromDefaultLanguageOnly: Indicate if to only execute the featured image update when the origin post has the default language of the site. It is `true` by default.
#
# *********************************************************************
#
# === Description ===
#
# This Persisted GraphQL query is an integration for Polylang.
#
# It takes an origin post, queries its featured image,
# and updates its translation posts with the corresponding
# featured image for the corresponding language,
# as set-up via Polylang. If there is no translation for that
# image, the same image from the origin post is set.
#
# If the origin post does not have a featured image,
# it removes it from all translation posts.
#
# All translation posts must have the  `draft` status
# to be updated. To update posts with any other status, use
# variable `$statusToUpdate` (for instance, passing value
# `publish`, `pending` or `any`)
#
# By default, the origin post must have the default language
# of the site, or the update will not be executed.
# To trigger the update from any language, pass variable
# `$triggerUpdateFromDefaultLanguageOnly` with `false`.
#
########################################################################
query InitializeVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  emptyString: _echo(value: null)
    @export(as: "fromLanguage")
 
  emptyID: _echo(value: null)
    @export(as: "originPostFeaturedImageID")
 
  emptyBool: _echo(value: false)
    @export(as: "hasFeaturedImage")
    @export(as: "executeUpdate")
    @export(as: "hasOriginPostTranslationPosts")
  
  emptyArray: _echo(value: [])
    @export(as: "originPostTranslationPostIds")
  
  emptyJSON: _echo(value: {})
    @export(as: "featuredImageTranslationLanguageIDs")
}
 
query ExportDataFromOriginPost($postId: ID!)
  @depends(on: "InitializeVariables")
{
  defaultLanguage: polylangDefaultLanguage {
    code @export(as: "defaultLanguage")
  }
 
  languages: polylangLanguages {
    code @export(
      as: "languageLocaleCodes"
      type: DICTIONARY
    )
  }
 
  originPost: post(by: { id: $postId }, status: any) {
    id
 
    polylangLanguage {
      code @export(as: "fromLanguage")
    }
    
    polylangTranslationLanguageIDs
    translationPostIds: _objectValues(object: $__polylangTranslationLanguageIDs)
      @export(as: "originPostTranslationPostIds")
 
    hasTranslationPosts: _notEmpty(value: $__translationPostIds)
      @export(as: "hasOriginPostTranslationPosts")
 
    featuredImage @export(as: "originPostFeaturedImageID") {
      id
 
      polylangTranslationLanguageIDs
        @export(as: "featuredImageTranslationLanguageIDs")
    }
 
    hasFeaturedImage: _notNull(value: $__featuredImage)
      @export(as: "hasFeaturedImage")
  }
}
 
query FilterTranslationPostsToUpdate(
  $statusToUpdate: CustomPostStatusEnum! = draft
  $triggerUpdateFromDefaultLanguageOnly: Boolean! = true
)
  @depends(on: "ExportDataFromOriginPost")
  @include(if: $hasOriginPostTranslationPosts)
{
  translationPosts: posts(filter: { ids: $originPostTranslationPostIds, status: $statusToUpdate } ) {
    id
  }
 
  hasTranslationPosts: _notEmpty(value: $__translationPosts)
 
  originPostHasDefaultLanguage: _equals(
    value1: $defaultLanguage,
    value2: $fromLanguage
  )
 
  canTriggerUpdateFromOriginPost: _if(
    condition: $triggerUpdateFromDefaultLanguageOnly,
    then: $__originPostHasDefaultLanguage,
    else: true
  )
 
  executeUpdate: _and(values: [
    $__hasTranslationPosts,
    $__canTriggerUpdateFromOriginPost
  ])
    @export(as: "executeUpdate")
}
 
mutation UpdateOrRemoveFeaturedImageForTranslationPosts(
  $statusToUpdate: CustomPostStatusEnum! = draft
)
  @depends(on: "FilterTranslationPostsToUpdate")
  @include(if: $executeUpdate)
{
  updateFeaturedImageInTranslationPosts: posts(filter: { ids: $originPostTranslationPostIds, status: $statusToUpdate } )
    @include(if: $hasFeaturedImage)
  {
    id
 
    polylangLanguageLocale: polylangLanguage {
      code
    }
    polylangLanguage: _objectProperty(
      object: $languageLocaleCodes,
      by: { key: $__polylangLanguageLocale }
    )
 
    translationFeaturedImageID: _objectProperty(
      object: $featuredImageTranslationLanguageIDs,
      by: {
        key: $__polylangLanguage
      }
      failIfNonExistingKeyOrPath: false
    )
 
    hasTranslationFeaturedImageID: _notNull(value: $__translationFeaturedImageID)
 
    translationOrDefaultFeaturedImageID: _if(
      condition: $__hasTranslationFeaturedImageID,
      then: $__translationFeaturedImageID,
      else: $originPostFeaturedImageID
    )
      
    setFeaturedImage(input: {
      mediaItemBy: {
        id: $__translationOrDefaultFeaturedImageID
      }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      customPost {
        __typename
        ...on CustomPost {
          featuredImage {
            id
            polylangLanguage {
              code
            }
          }
        }
      }
    }
  }
 
  removeFeaturedImageFromTranslationPosts: posts(filter: { ids: $originPostTranslationPostIds, status: $statusToUpdate } )
    @skip(if: $hasFeaturedImage)
  {
    id      
    removeFeaturedImage {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      customPost {
        __typename
        ...on CustomPost {
          featuredImage {
            id
          }
        }
      }
    }
  }
}