Search and replace multiple strings in all posts

🔥 This query is already included in Gato GraphQL, as Persisted Query “Replace strings in posts”.

Update multiple posts with a single operation, searching/replacing content.

Given the posts indicated by variable $postIds, this query will replace all the strings in $search with those in $replace, in the post's title, excerpt and content.

This query requires the endpoint to have Nested Mutations enabled.

query TransformAndExportData(
$postIds: [ID!]!
$search: [String!]!
$replace: [String!]!
) {
posts: posts(
filter: { ids: $postIds }
pagination: { limit: -1 }
sort: { by: ID, order: ASC }
) {
id
rawTitle
rawContent
rawExcerpt
@strReplaceMultiple(
search: $search
replaceWith: $replace
affectAdditionalFieldsUnderPos: [1, 2]
)
@deferredExport(
as: "postAdaptedSources"
type: DICTIONARY
affectAdditionalFieldsUnderPos: [1, 2]
)
}
}

query AdaptDataForMutationInput
@depends(on: "TransformAndExportData")
{
postInputs: _echo(value: $postAdaptedSources)
@underEachJSONObjectProperty(
passValueOnwardsAs: "adaptedSource",
affectDirectivesUnderPos: [1, 2, 3, 4]
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawTitle"
}
},
passOnwardsAs: "adaptedTitle"
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawExcerpt"
}
},
passOnwardsAs: "adaptedExcerpt"
)
@applyField(
name: "_objectProperty",
arguments: {
object: $adaptedSource,
by: {
key: "rawContent"
}
},
passOnwardsAs: "adaptedContent"
)
@applyField(
name: "_echo",
arguments: {
value: {
title: $adaptedTitle,
excerpt: $adaptedExcerpt,
contentAs: {
html: $adaptedContent
}
}
},
setResultInResponse: true
)
@export(as: "postInputs")
}

mutation SearchAndReplaceStringsInAllPosts(
$postIds: [ID!]!
)
@depends(on: "AdaptDataForMutationInput")
{
adaptedPosts: posts(
filter: { ids: $postIds }
pagination: { limit: -1 }
sort: { by: ID, order: ASC }
) {
id
postInput: _objectProperty(
object: $postInputs,
by: { key: $__id }
) @remove
update(input: $__postInput) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
title
content
excerpt
}
}
}
}

Extensions required for this query permalink

  1. Field Response Removal permalink

    Remove the output of a field from the response.

  2. Field To Input permalink

    Retrieve the value of a field, manipulate it, and input it into another field or directive, all within the same operation.

  3. Multiple Query Execution permalink

    Combine multiple queries into a single query, sharing state across them and executing them in the requested order.

  4. PHP Functions via Schema permalink

    Manipulate the field output using standard programming language functions available in PHP.

Bundles containing all extensions required for this query permalink

  1. “All in One Toolbox for WordPress” Bundle permalink

    Achieve all superpowers: All of Gato GraphQL extensions, in a single plugin

  2. “Automated Content Translation & Sync for WordPress Multisite” Bundle permalink

    Automatically create a translation of a newly-published post using the Google Translate API, for every language site on a WordPress multisite

  3. “Better WordPress Webhooks” Bundle permalink

    Easily create webhooks to process incoming data from any source or service using advanced tools, directly within the wp-admin

  4. “Private GraphQL Server for WordPress” Bundle permalink

    Use GraphQL to power your application (blocks, themes and plugins), internally fetching data without exposing a public endpoint

  5. “Selective Content Import, Export & Sync for WordPress” Bundle permalink

    Import hundreds of records into your WordPress site from another site or service (such as Google Sheets), and selectively export entries to another site

  6. “Simplest WordPress Content Translation” Bundle permalink

    Translate your content into over 130 languages using the Google Translate API, without adding extra tables or inner joins to the DB

  7. “Tailored WordPress Automator” Bundle permalink

    Create workflows to automate tasks (to transform data, automatically caption images, send notifications, and more)

  8. “Versatile WordPress Request API” Bundle permalink

    Interact with any external API and cloud service, posting and fetching data to/from them

Tutorial lessons explaining how the query works permalink