Consultar dados do WordPressPages
Pages
Estes são exemplos de queries para buscar dados de páginas.
Buscando páginas
Uma única página:
query {
page(by: { id: 2 }) {
id
title
content
url
date
}
}Uma lista de páginas:
query {
pages(pagination: { limit: 5 }) {
id
title
excerpt
url
dateStr(format: "d/m/Y")
}
}Páginas de nível superior com seus filhos:
query {
pages(filter: { parentID: 0 }) {
...PageProps
children {
...PageProps
children(pagination: { limit: 3 }) {
...PageProps
}
}
}
}
fragment PageProps on Page {
id
title
date
urlPath
}Buscando as páginas do usuário logado
Os campos page, pages e pageCount recuperam apenas páginas com status "publish".
Para buscar páginas do usuário logado, com qualquer status ("publish", "pending", "draft" ou "trash"), use estes campos:
myPagemyPagesmyPageCount
query {
myPages(filter: { status: [draft, pending] }) {
id
title
status
}
}Criando páginas
Apenas usuários logados podem criar páginas.
mutation {
createPage(
input: {
title: "Hi there!"
contentAs: { html: "How do you like it?" }
status: draft
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
...on GenericErrorPayload {
code
}
}
pageID
page {
status
title
content
url
date
author {
id
name
}
}
}
}Atualizando páginas
Apenas usuários com as permissões correspondentes podem editar páginas.
mutation {
updatePage(
input: {
id: 2,
title: "This is my new title",
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
...on GenericErrorPayload {
code
}
}
page {
id
title
}
}
}Esta query usa mutations aninhadas para atualizar a página:
mutation {
page(by: { id: 2 }) {
originalTitle: title
update(input: {
title: "This is my new title",
contentAs: { html: "This rocks!" }
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
page {
newTitle: title
content
}
}
}
}