Consultar dados do WordPressArtigos
Artigos
Estes são exemplos de queries para buscar e modificar dados de posts.
Buscando posts
Um único post, com autor e tags:
query {
post(by: { id: 1 }) {
title
content
url
date
author {
id
name
}
tags {
id
name
}
}
}Uma lista de 5 posts com seus comentários:
query {
posts(pagination: { limit: 5 }) {
id
title
excerpt
url
dateStr(format: "d/m/Y")
comments(pagination: { limit: 5 }) {
id
date
content
}
}
}Uma lista de posts predefinidos:
query {
posts(filter: { ids: [1499, 1657] }) {
id
title
excerpt
url
date
}
}Filtrando posts:
query {
posts(
filter: { search: "wordpress", dateQuery: { after: "2019-06-01" } },
sort: { order: ASC, by: TITLE }
) {
id
title
excerpt
url
status
}
}Contando resultados de posts:
query {
postCount(
filter: { search: "api" }
)
}Paginando posts:
query {
posts(
pagination: {
limit: 5,
offset: 5
}
) {
id
title
}
}Posts com tags:
query {
posts(
filter: { tagSlugs: ["graphql", "wordpress", "plugin"] }
) {
id
title
}
}Posts com categorias:
query {
posts(
filter: { categoryIDs: [50, 190] }
) {
id
title
}
}Buscando valores meta:
query {
posts {
title
metaValue(
key: "_wp_page_template",
)
}
}Buscando os posts do usuário conectado
Os campos post, posts e postCount recuperam apenas posts com status "publish".
Para buscar posts do usuário conectado, com qualquer status ("publish", "pending", "draft" ou "trash"), use estes campos:
myPostmyPostsmyPostCount
query {
myPosts(filter: { status: [draft, pending] }) {
id
title
status
}
}Criando posts
Somente usuários conectados podem criar posts.
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
}
}
}
}Atualizando posts
Somente usuários com as permissões correspondentes podem editar posts.
mutation {
updatePost(
input: {
id: 1,
title: "This is my new title",
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
...on GenericErrorPayload {
code
}
}
post {
id
title
}
}
}Esta query utiliza mutations aninhadas para atualizar o post:
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
}
}
}
}