WordPress-gegevens opvragenArtikelen
Artikelen
Dit zijn voorbeelden van queries om postgegevens op te halen en te wijzigen.
Posts ophalen
Een enkele post, met auteur en tags:
query {
post(by: { id: 1 }) {
title
content
url
date
author {
id
name
}
tags {
id
name
}
}
}Een lijst van 5 posts met hun reacties:
query {
posts(pagination: { limit: 5 }) {
id
title
excerpt
url
dateStr(format: "d/m/Y")
comments(pagination: { limit: 5 }) {
id
date
content
}
}
}Een lijst van vooraf gedefinieerde posts:
query {
posts(filter: { ids: [1499, 1657] }) {
id
title
excerpt
url
date
}
}Posts filteren:
query {
posts(
filter: { search: "wordpress", dateQuery: { after: "2019-06-01" } },
sort: { order: ASC, by: TITLE }
) {
id
title
excerpt
url
status
}
}Aantal postresultaten tellen:
query {
postCount(
filter: { search: "api" }
)
}Posts pagineren:
query {
posts(
pagination: {
limit: 5,
offset: 5
}
) {
id
title
}
}Posts met tags:
query {
posts(
filter: { tagSlugs: ["graphql", "wordpress", "plugin"] }
) {
id
title
}
}Posts met categorieƫn:
query {
posts(
filter: { categoryIDs: [50, 190] }
) {
id
title
}
}Meta-waarden ophalen:
query {
posts {
title
metaValue(
key: "_wp_page_template",
)
}
}Posts van de ingelogde gebruiker ophalen
De velden post, posts en postCount halen alleen posts op met de status "publish".
Om posts van de ingelogde gebruiker op te halen met elke status ("publish", "pending", "draft" of "trash"), gebruik je deze velden:
myPostmyPostsmyPostCount
query {
myPosts(filter: { status: [draft, pending] }) {
id
title
status
}
}Posts aanmaken
Alleen ingelogde gebruikers kunnen posts aanmaken.
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
}
}
}
}Posts bijwerken
Alleen gebruikers met de bijbehorende rechten kunnen posts bewerken.
mutation {
updatePost(
input: {
id: 1,
title: "This is my new title",
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
...on GenericErrorPayload {
code
}
}
post {
id
title
}
}
}Deze query gebruikt geneste mutaties om de post bij te werken:
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
}
}
}
}