การ Query ข้อมูล WordPress
การ Query ข้อมูล WordPressโพสต์

โพสต์

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

การดึงข้อมูลโพสต์

โพสต์เดียวพร้อมผู้เขียนและแท็ก:

query {
  post(by: { id: 1 }) {
    title
    content
    url
    date
    author {
      id
      name
    }
    tags {
      id
      name
    }
  }
}

รายการโพสต์ 5 รายการพร้อมความคิดเห็น:

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

รายการโพสต์ที่กำหนดไว้ล่วงหน้า:

query {
  posts(filter: { ids: [1499, 1657] }) {
    id
    title
    excerpt
    url
    date
  }
}

การกรองโพสต์:

query {
  posts(
    filter: { search: "wordpress", dateQuery: { after: "2019-06-01" } },
    sort: { order: ASC, by: TITLE }
  ) {
    id
    title
    excerpt
    url
    status
  }
}

การนับจำนวนโพสต์:

query {
  postCount(
    filter: { search: "api" }
  )
}

การแบ่งหน้าโพสต์:

query {
  posts(
    pagination: {
      limit: 5,
      offset: 5
    }
  ) {
    id
    title
  }
}

โพสต์ที่มีแท็ก:

query {
  posts(
    filter: { tagSlugs: ["graphql", "wordpress", "plugin"] }
  ) {
    id
    title
  }
}

โพสต์ที่มีหมวดหมู่:

query {
  posts(
    filter: { categoryIDs: [50, 190] }
  ) {
    id
    title
  }
}

การดึงค่า meta:

query {
  posts {
    title
    metaValue(
      key: "_wp_page_template",
    )
  }
}

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

ฟิลด์ post, posts และ postCount จะดึงเฉพาะโพสต์ที่มีสถานะ "publish" เท่านั้น

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

  • myPost
  • myPosts
  • myPostCount
query {
  myPosts(filter: { status: [draft, pending] }) {
    id
    title
    status
  }
}

การสร้างโพสต์

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

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

การอัปเดตโพสต์

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

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

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

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