ไลบรารี Queries
ไลบรารี Queriesทำสำเนาโพสต์ Bricks

ทำสำเนาโพสต์ Bricks

query นี้จะทำสำเนาของ custom post ของ Bricks (รวมถึงชื่อ เนื้อหา เกริ่นนำ ผู้เขียน รูปภาพเด่น และเมตาดาต้า) และสร้างรหัส element ของ Bricks ใหม่สำหรับ custom post ที่ถูกทำสำเนา

query นี้จำเป็นต้องเปิดใช้งาน Bricks extension

query นี้จำเป็นต้องใช้ตัวแปรต่อไปนี้:

  • customPostId: ID ของ custom post ของ Bricks ที่จะทำสำเนา
query InitializeDynamicVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  authorID: _echo(value: null)
    @export(as: "authorID")
    @remove
 
  featuredImageID: _echo(value: null)
    @export(as: "featuredImageID")
    @remove
 
  meta: _echo(value: {})
    @export(as: "meta")
    @remove
 
  bricksIsEnabledForCustomPostType: _echo(value: false)
    @export(as: "bricksIsEnabledForCustomPostType")
    @remove
}
 
query GetBricksCustomPostAndExportData($customPostId: ID!)
  @depends(on: "InitializeDynamicVariables")
{
  customPost(by: { id: $customPostId }, status: any) {
    bricksIsEnabledForCustomPostType
      @export(as: "bricksIsEnabledForCustomPostType")
 
    # Fields not to be duplicated
    id
    slug
    date
    status
 
    # Fields to be duplicated
    author {
      id @export(as: "authorID")
    }
    customPostType @export(as: "customPostType")
    rawContent @export(as: "rawContent")
    rawExcerpt @export(as: "excerpt")
    featuredImage {
      id @export(as: "featuredImageID")
    }
    rawTitle @export(as: "title")
 
    metaKeys(filter: { exclude: ["_thumbnail_id", "_edit_last"] })
    meta(keys: $__metaKeys) 
      @export(as: "meta")
  }
}
 
mutation DuplicateBricksCustomPost
  @depends(on: "GetBricksCustomPostAndExportData")
  @include(if: $bricksIsEnabledForCustomPostType)
{
  createCustomPost(input: {
    status: draft,
    customPostType: $customPostType,
    authorBy: {
      id: $authorID
    },
    contentAs: {
      html: $rawContent
    },
    excerpt: $excerpt
    featuredImageBy: {
      id: $featuredImageID
    },
    title: $title,
    meta: $meta
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      # Fields not to be duplicated
      id @export(as: "newCustomPostId")
      slug
      date
      status
 
      # Fields to be duplicated
      customPostType
      author {
        id
      }
      rawContent
      excerpt
      featuredImage {
        id
      }
      title
      
      metaKeys(filter: { exclude: ["_thumbnail_id", "_edit_last"] })
      meta(keys: $__metaKeys)
    }
  }
}
 
mutation RegenerateDuplicatedCustomPostBricksData
  @depends(on: "DuplicateBricksCustomPost")
  @include(if: $bricksIsEnabledForCustomPostType)
{
  bricksRegenerateCustomPostElementIDSet(input: {
    customPostID: $newCustomPostId
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}