WordPress-Daten abfragenKommentare
Kommentare
Dies sind Beispiele für queries zum Abrufen und Hinzufügen von Kommentaren.
Kommentare abrufen
Kommentare eines Beitrags:
query {
post(by: { id: 1 }) {
comments {
id
content
author {
name
}
parent {
id
}
}
}
}Kommentare und ihre Antworten, über mehrere Ebenen:
query {
post(by: { id: 1499 }) {
comments(pagination: { limit: 5 }) {
...CommentFields
responses {
...CommentFields
responses {
...CommentFields
}
}
}
}
}
fragment CommentFields on Comment {
id
date
content
}Kommentare filtern:
{
posts {
title
comments(
filter: { search: "insight" }
) {
id
content
}
}
}Kommentarergebnisse zählen:
{
posts {
id
commentCount
}
}Kommentare paginieren:
{
posts {
id
comments(
pagination: {
limit: 3,
offset: 3
}
) {
id
date
content
}
}
}Alle Kommentare der Website von einem bestimmten Benutzer:
{
commentCount(filter: { authorIDs: [1], parentID: null })
comments(filter: { authorIDs: [1], parentID: null }, pagination: { limit: -1 }) {
id
date
content
}
}Ein bestimmter Kommentar:
{
comment(by: { id: 272 }) {
id
date
content
author {
id
name
}
}
}Meta-Werte abrufen:
{
posts {
id
comments{
id
metaValue(
key:"someKey"
)
}
}
}Einen Kommentar hinzufügen
Eingeloggte oder nicht eingeloggte Benutzer können Kommentare hinzufügen:
mutation {
addCommentToCustomPost(
input: { customPostID: 1459, commentAs: { html: "Lovely tango!" } }
) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
commentID
comment {
date
content
author {
id
name
}
}
}
}Wir können auch verschachtelte Mutations verwenden:
mutation {
post(by: { id: 1459 }) {
id
title
addComment(input: { commentAs: { html: "Lovely tango!" } }) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
commentID
comment {
date
content
author {
id
name
}
}
}
}
}Auf einen Kommentar antworten
Ähnlich wie das Hinzufügen eines Kommentars, aber mit dem zusätzlichen Argument parentCommentID:
mutation {
addCommentToCustomPost(
input: {
customPostID: 1459
parentCommentID: 272
commentAs: { html: "Hi to you too" }
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
commentID
comment {
date
content
author {
id
name
}
}
}
}Oder das spezifischere Feld replyComment verwenden:
mutation {
replyComment(input: { parentCommentID: 272, commentAs: { html: "Hi to you too" } }) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
commentID
comment {
date
content
author {
id
name
}
}
}
}Oder mit verschachtelten Mutations zum übergeordneten Kommentar navigieren:
mutation {
post(by: { id: 1459 }) {
comments(filter: { ids: 272 }) {
id
content
reply(input: { commentAs: { html: "Everything good?" } }) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
commentID
comment {
date
content
}
}
}
}
}