Zapytania o dane WordPressArtykuły
Artykuły
Oto przykłady queries do pobierania i modyfikowania danych postów.
Pobieranie postów
Pojedynczy post z autorem i tagami:
query {
post(by: { id: 1 }) {
title
content
url
date
author {
id
name
}
tags {
id
name
}
}
}Lista 5 postów z ich komentarzami:
query {
posts(pagination: { limit: 5 }) {
id
title
excerpt
url
dateStr(format: "d/m/Y")
comments(pagination: { limit: 5 }) {
id
date
content
}
}
}Lista predefiniowanych postów:
query {
posts(filter: { ids: [1499, 1657] }) {
id
title
excerpt
url
date
}
}Filtrowanie postów:
query {
posts(
filter: { search: "wordpress", dateQuery: { after: "2019-06-01" } },
sort: { order: ASC, by: TITLE }
) {
id
title
excerpt
url
status
}
}Zliczanie wyników postów:
query {
postCount(
filter: { search: "api" }
)
}Stronicowanie postów:
query {
posts(
pagination: {
limit: 5,
offset: 5
}
) {
id
title
}
}Posty z tagami:
query {
posts(
filter: { tagSlugs: ["graphql", "wordpress", "plugin"] }
) {
id
title
}
}Posty z kategoriami:
query {
posts(
filter: { categoryIDs: [50, 190] }
) {
id
title
}
}Pobieranie wartości meta:
query {
posts {
title
metaValue(
key: "_wp_page_template",
)
}
}Pobieranie postów zalogowanego użytkownika
Pola post, posts i postCount pobierają tylko posty o statusie "publish".
Aby pobrać posty zalogowanego użytkownika z dowolnym statusem ("publish", "pending", "draft" lub "trash"), użyj tych pól:
myPostmyPostsmyPostCount
query {
myPosts(filter: { status: [draft, pending] }) {
id
title
status
}
}Tworzenie postów
Tylko zalogowani użytkownicy mogą tworzyć posty.
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
}
}
}
}Aktualizowanie postów
Tylko użytkownicy z odpowiednimi uprawnieniami mogą edytować posty.
mutation {
updatePost(
input: {
id: 1,
title: "This is my new title",
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
...on GenericErrorPayload {
code
}
}
post {
id
title
}
}
}Ta query używa zagnieżdżonych mutacji do aktualizacji posta:
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
}
}
}
}