This query identifies the nth block of a given type ("wp:paragraph"
by default) in all posts, and places the provided custom block's HTML content right after it.
Variable $injectBlockMarkup
must receive the full HMTL markup for the block (with quotes escaped for the JSON input). Eg:
<!-- 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 -->
Copy
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
}
}
}
}
Copy