Interrogare i dati di WordPress
Interrogare i dati di WordPressPages

Pages

Ecco alcuni esempi di queries per recuperare i dati delle pagine.

Recuperare le pagine

Una singola pagina:

query {
  page(by: { id: 2 }) {
    id
    title
    content
    url
    date
  }
}

Un elenco di pagine:

query {
  pages(pagination: { limit: 5 }) {
    id
    title
    excerpt
    url
    dateStr(format: "d/m/Y")
  }
}

Pagine di primo livello con i loro figli:

query {
  pages(filter: { parentID: 0 }) {
    ...PageProps
    children {
      ...PageProps
      children(pagination: { limit: 3 }) {
        ...PageProps
      }
    }
  }
}
 
fragment PageProps on Page {
  id
  title
  date
  urlPath
}

Recuperare le pagine dell'utente connesso

I campi page, pages e pageCount recuperano solo le pagine con stato "publish".

Per recuperare le pagine dell'utente connesso, con qualsiasi stato ("publish", "pending", "draft" o "trash"), utilizza questi campi:

  • myPage
  • myPages
  • myPageCount
query {
  myPages(filter: { status: [draft, pending] }) {
    id
    title
    status
  }
}

Creare pagine

Solo gli utenti connessi possono creare pagine.

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
      }
    }
  }
}

Aggiornare le pagine

Solo gli utenti che dispongono delle capacità corrispondenti possono modificare le pagine.

mutation {
  updatePage(
    input: {
      id: 2,
      title: "This is my new title",
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    page {
      id
      title
    }
  }
}

Questa query utilizza mutations annidate per aggiornare la pagina:

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
      }
    }
  }
}