การ Query ข้อมูล WordPress
การ Query ข้อมูล WordPressเพจ

เพจ

ต่อไปนี้คือตัวอย่าง queries สำหรับดึงข้อมูลเพจ

การดึงข้อมูลเพจ

เพจเดียว:

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

รายการเพจ:

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

เพจระดับบนสุดพร้อมเพจลูก:

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

การดึงข้อมูลเพจของผู้ใช้ที่เข้าสู่ระบบ

ฟิลด์ page, pages และ pageCount จะดึงเฉพาะเพจที่มีสถานะ "publish" เท่านั้น

หากต้องการดึงเพจของผู้ใช้ที่เข้าสู่ระบบด้วยสถานะใดก็ได้ ("publish", "pending", "draft" หรือ "trash") ให้ใช้ฟิลด์เหล่านี้:

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

การสร้างเพจ

เฉพาะผู้ใช้ที่เข้าสู่ระบบเท่านั้นที่สามารถสร้างเพจได้

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

การอัปเดตเพจ

เฉพาะผู้ใช้ที่มีสิทธิ์ที่เกี่ยวข้องเท่านั้นที่สามารถแก้ไขเพจได้

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

queries นี้ใช้ nested mutations เพื่ออัปเดตเพจ:

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