Queries Library
Queries LibraryReplace an old post slug with a new post slug in all posts

Replace an old post slug with a new post slug in all posts

After changing the slug of a post, execute this query to convert all content to point to the new URL.

This query requires the endpoint to have Nested Mutations enabled.

query ExportData(
  $oldPostSlug: String!
  $newPostSlug: String!
) {
  siteURL: optionValue(name: "siteurl")
 
  oldPostURL: _strAppend(
    after: $__siteURL,
    append: $oldPostSlug
  ) @export(as: "oldPostURL")
 
  newPostURL: _strAppend(
    after: $__siteURL,
    append: $newPostSlug
  ) @export(as: "newPostURL")
}
 
mutation ReplaceOldWithNewSlugInPosts
  @depends(on: "ExportData")
{
  posts(
    filter: {
      search: $oldPostURL
    },
    pagination: {
      limit: -1
    }
  ) {
    id
    rawContent
    adaptedRawContent: _strReplace(
      search: $oldPostURL
      replaceWith: $newPostURL
      in: $__rawContent
    )
    update(input: {
      contentAs: { html: $__adaptedRawContent }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        rawContent
      }
    }
  }
}