Coding with an API
Coding with an APIInjecting multiple resources into WP-CLI

Injecting multiple resources into WP-CLI

In guide Complementing WP-CLI, we only retrieved (and injected into WP-CLI) a single user ID. Now, let's retrieve multiple user IDs while executing a single GraphQL query.

In this GraphQL query:

  • We remove the pagination argument from the query, as to retrieve the list of all the users with Spanish locale
  • We use Multiple Query Execution to export a list of the user IDs, under dynamic variable $userIDs
  • We print the elements of this array with _arrayJoin, joining the items with a space in between, under alias spanishLocaleUserIDs
  • We execute the operation FormatAndPrintData
# This query is stored in file "find-multiple-users-with-spanish-locale.gql"
query RetrieveData {
    filter: {
      metaQuery: {
        key: "locale",
        compareBy: {
          stringValue: {
            value: "es_[A-Z]+"
            operator: REGEXP
  ) {
    id @export(as: "userIDs", type: LIST)
    locale: metaValue(key: "locale")
query FormatAndPrintData @depends(on: "RetrieveData") {
  spanishLocaleUserIDs: _arrayJoin(
    array: $userIDs,
    separator: " "

The response to this query will be:

  "data": {
    "users": [
        "id": 3,
        "name": "Subscriber Bennett",
        "locale": "es_AR"
        "id": 2,
        "name": "Blogger Davenport",
        "locale": "es_ES"
    "spanishLocaleUserIDs": "3 2"

When executing the query, the dictionary in the body of the request must indicate the name of the operation to execute ("FormatAndPrintData"):

GRAPHQL_QUERY=$(cat find-multiple-users-with-spanish-locale.gql)
GRAPHQL_BODY="{\"operationName\": \"FormatAndPrintData\", \"query\": \"$(echo $GRAPHQL_QUERY | tr '\n' ' ' | sed 's/"/\\"/g')\"}"
  -X POST \
  -H "Content-Type: application/json" \

We must also adapt the regex (due to the new alias, the space in between the IDs, and the quotes around this string):

  | grep -E -o '"spanishLocaleUserIDs\":"((\d|\s)+)"' \
  | cut -d':' -f2- | cut -d'"' -f2- | rev | cut -d'"' -f2- | rev)

Printing the contents of variable SPANISH_LOCALE_USER_IDS, we get all the IDs, separated with a space:

# Response:
# 3 2

We can now inject all IDs together to the WP-CLI command (if it supports it), or iterate them and execute the command for each of them:

for USER_ID in $(echo $SPANISH_LOCALE_USER_IDS); do wp user update "$(echo $USER_ID)" --locale=fr_FR; done