쿼리 라이브러리
쿼리 라이브러리모든 게시물에 블록 삽입하기

모든 게시물에 블록 삽입하기

이 쿼리는 모든 게시물에서 지정한 타입의 n번째 블록(기본값은 "wp:paragraph")을 찾아, 제공된 사용자 정의 블록의 HTML 콘텐츠를 그 바로 뒤에 배치합니다.

변수 $injectBlockMarkup에는 블록의 전체 HTML 마크업(JSON 입력을 위해 따옴표를 이스케이프한 상태)을 전달해야 합니다. 예시:

<!-- mycompany:black-friday-campaign-video --><figure class=\"wp-block-video\"><video controls src=\"https://mysite.com/videos/BlackFriday2023.mp4\"></video></figure><!-- /mycompany:black-friday-campaign-video -->

이 쿼리를 실행하려면 엔드포인트에서 중첩 뮤테이션이 활성화되어 있어야 합니다.

query CreateRegex(
  $searchBlockType: String! = "wp:paragraph"
  $injectAfterBlockCount: Int = 1
  $injectBlockMarkup: String!
) {
  endingBlockMarkup: _sprintf(
    string: "<!-- /%s -->",
    values: [$searchBlockType]
  )
    @remove
  endingBlockMarkupArray: _arrayPad(
    array: [],
    length: $injectAfterBlockCount,
    value: $__endingBlockMarkup
  )
    @remove
  regexString: _arrayJoin(
    array: $__endingBlockMarkupArray,
    separator: "[\\s\\S]+"
  )
    @remove
  regex: _sprintf(
    string: "#(%s)#U",
    values: [$__regexString]
  )
    @export(as: "regex")
    @remove
  replaceWith: _sprintf(
    string: "$1%s",
    values: [$injectBlockMarkup]
  )
    @export(as: "replaceWith")
    @remove
}
 
mutation InsertBlockInAllPosts
  @depends(on: "CreateRegex")
{
  posts: posts(
    pagination: { limit: -1 }
  ) {
    id
    rawContent
    adaptedRawContent: _strRegexReplace(
      in: $__rawContent,
      searchRegex: $regex,
      replaceWith: $replaceWith,
      limit: 1
    )
    update(input: {
      contentAs: { html: $__adaptedRawContent },
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        rawContent
      }
    }
  }
}