Queries Library
Queries LibraryDuplicate a Bricks post

Duplicate a Bricks post

This query duplicates a Bricks custom post (including its title, content, excerpt, author, featured image, and metadata), and regenerates the Bricks element IDs for the duplicated custom post.

This query requires the Bricks extension to be enabled.

The query requires the following variables:

  • customPostId: The ID of the Bricks custom post to duplicate.
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
      }
    }
  }
}