WordPress-Daten abfragen
WordPress-Daten abfragenMeta-Werte

Meta-Werte

Mehr dazu findest du in der Anleitung Arbeiten mit Meta-Werten.

Das sind Beispiele für queries zum Abrufen von Metadaten und zum Filtern von Ergebnissen nach Meta.

Meta abfragen

Den einzelnen Meta-Wert _thumbnail_id aus Posts abrufen:

{
  posts {
    id
    title
    metaValue(key: "_thumbnail_id")
  }
}

Den Array-Meta-Wert upvotes aus Kommentaren abrufen:

{
  comments {
    id
    content
    upvotes: metaValues(key: "upvotes")
  }
}

Nach Meta filtern

Posts filtern, bei denen der Meta-Schlüssel _thumbnail_id vorhanden ist:

{
  posts(filter: {
    metaQuery: {
      key: "_thumbnail_id",
      compareBy:{
        key: {
          operator: EXISTS
        }
      }
    }
  }) {
    id
    title
    metaValue(key: "_thumbnail_id")
  }
}

Benutzer filtern, bei denen der Meta-Eintrag nickname einen bestimmten Wert hat:

{
  users(filter: {
    metaQuery: {
      key: "nickname",
      compareBy:{
        stringValue: {
          value: "leo"
          operator: EQUALS
        }
      }
    }
  }) {
    id
    name
    metaValue(key: "nickname")
  }
}

Kommentare filtern, bei denen der Meta-Eintrag upvotes (ein Array von Ganzzahlen) den Wert 4 oder 5 enthält:

{
  comments(filter: {
    metaQuery: [
      {
        relation: OR
        key: "upvotes",
        compareBy: {
          arrayValue: {
            value: 4
            operator: IN
          }
        }
      },
      {
        key: "upvotes",
        compareBy: {
          arrayValue: {
            value: 5
            operator: IN
          }
        }
      }
  ]}) {
    id
    content
    upvotes: metaValues(key: "upvotes")
  }
}

Meta hinzufügen

Du kannst Meta-Einträge zu Custom Posts, Tags, Kategorien, Kommentaren und Benutzern hinzufügen.

Diese query fügt einen Meta-Eintrag zum Post mit der ID 4 hinzu:

mutation {
  addCustomPostMeta(input: {
    id: 4
    key: "some_key"
    value: "Some value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      metaValue(key: "some_key") 
    }
  }
}

Diese query fügt denselben Meta-Schlüssel mit verschiedenen Werten zu verschiedenen Posts hinzu, als Massenoperation:

mutation {
  addCustomPostMetas(inputs: [
    {
      id: 4
      key: "some_key"
      value: "Some value"
    },
    {
      id: 5
      key: "some_key"
      value: "Some other value"
    },
    {
      id: 6
      key: "some_key"
      value: "Yet another value"
    }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      metaValue(key: "some_key") 
    }
  }
}

Meta aktualisieren

Einen Kategorie-Meta-Eintrag aktualisieren:

mutation {
  updateCategoryMeta(input: {
    id: 20
    key: "_source"
    value: "Updated source value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      __typename
      id
      metaValue(key: "_source") 
    }
  }
}

Diese query verwendet verschachtelte Mutationen, um einen Meta-Wert in einem Post zu aktualisieren:

mutation {
  post(by: {id: 1}) {
    updateMeta(input: {
      key: "some_key"
      value: "Updated description"
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        metaValue(key: "single_meta_key") 
      }
    }
  }
}

Meta löschen

Einen Meta-Eintrag aus einem Post löschen:

mutation {
  deletePostMeta(input: {
    id: 5
    key: "some_key"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

Denselben Meta-Eintrag aus mehreren Posts löschen:

mutation {
  deletePostMetas(inputs: [
    {
      id: 5
      key: "some_key"
    },
    {
      id: 6
      key: "some_key"
    }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

Mehrere Meta-Einträge auf einmal setzen

Du kannst mehrere Meta-Einträge auf einmal setzen, indem du ein JSON an die verschiedenen set{Entity}Meta-Mutationen übergibst:

mutation {
  setCustomPostMeta(input: {
    id: 4
    entries: {
      single_meta_key: [
        "This is a single entry",
      ],
      object_meta_key: [
        {
          key: "This is a key",
          value: "This is a value",
        },
      ],
      array_meta_key: [
        "This is a string",
        "This is another string",
      ],
      object_array_meta_key: [
        [
          {
            key: "This is a key 1",
            value: "This is a value 1",
          },
          {
            key: "This is a key 2",
            value: "This is a value 2",
          },
        ]
      ],
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      meta(keys: ["single_meta_key", "object_meta_key", "array_meta_key", "object_array_meta_key"])
    }
  }
}

Meta-Einträge beim Erstellen/Aktualisieren einer Entität setzen

Du kannst Meta-Einträge direkt beim Erstellen oder Aktualisieren eines Custom Posts, Tags, einer Kategorie oder eines Kommentars über den Parameter meta definieren.

Diese query setzt Meta beim Hinzufügen eines Kommentars:

mutation {
  addCommentToCustomPost(input: {
    customPostID: 1130
    commentAs: { html: "New comment" }
    meta: {
      some_meta_key: [
        "This is a single entry",
      ],
      another_meta_key: [
        "This is an array entry 1",
        "This is an array entry 2",
      ],
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    comment {
      id
      meta(keys: ["some_meta_key", "another_meta_key"]) 
    }
  }
}

Diese query injiziert die Meta in die verschachtelte Mutation Post.update:

mutation {
  post(by: {id: 1}) {
    update(input: {
      meta: {
        single_meta_key: [
          "This is an updated value",
        ]
      }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        metaValue(key: "single_meta_key") 
      }
    }
  }
}
Prev
Next