Interrogare i dati di WordPressArticoli
Articoli
Questi sono esempi di queries per recuperare e modificare i dati degli articoli.
Recuperare articoli
Un singolo articolo, con autore e tag:
query {
post(by: { id: 1 }) {
title
content
url
date
author {
id
name
}
tags {
id
name
}
}
}Un elenco di 5 articoli con i loro commenti:
query {
posts(pagination: { limit: 5 }) {
id
title
excerpt
url
dateStr(format: "d/m/Y")
comments(pagination: { limit: 5 }) {
id
date
content
}
}
}Un elenco di articoli predefiniti:
query {
posts(filter: { ids: [1499, 1657] }) {
id
title
excerpt
url
date
}
}Filtrare articoli:
query {
posts(
filter: { search: "wordpress", dateQuery: { after: "2019-06-01" } },
sort: { order: ASC, by: TITLE }
) {
id
title
excerpt
url
status
}
}Contare i risultati degli articoli:
query {
postCount(
filter: { search: "api" }
)
}Paginare gli articoli:
query {
posts(
pagination: {
limit: 5,
offset: 5
}
) {
id
title
}
}Articoli con tag:
query {
posts(
filter: { tagSlugs: ["graphql", "wordpress", "plugin"] }
) {
id
title
}
}Articoli con categorie:
query {
posts(
filter: { categoryIDs: [50, 190] }
) {
id
title
}
}Recuperare valori meta:
query {
posts {
title
metaValue(
key: "_wp_page_template",
)
}
}Recuperare gli articoli dell'utente connesso
I campi post, posts e postCount recuperano solo gli articoli con stato "publish".
Per recuperare gli articoli dell'utente connesso, con qualsiasi stato ("publish", "pending", "draft" o "trash"), utilizza questi campi:
myPostmyPostsmyPostCount
query {
myPosts(filter: { status: [draft, pending] }) {
id
title
status
}
}Creare articoli
Solo gli utenti connessi possono creare articoli.
mutation {
createPost(
input: {
title: "Hi there!"
contentAs: { html: "How do you like it?" }
status: draft
tags: ["demo", "plugin"]
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
...on GenericErrorPayload {
code
}
}
postID
post {
status
title
content
url
date
author {
id
name
}
tags {
id
name
}
}
}
}Aggiornare articoli
Solo gli utenti con le capacità corrispondenti possono modificare gli articoli.
mutation {
updatePost(
input: {
id: 1,
title: "This is my new title",
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
...on GenericErrorPayload {
code
}
}
post {
id
title
}
}
}Questa query utilizza mutation annidate per aggiornare l'articolo:
mutation {
post(by: { id: 1 }) {
originalTitle: title
update(input: {
title: "This is my new title",
contentAs: { html: "This rocks!" }
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
newTitle: title
content
}
}
}
}