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

แท็กโพสต์

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

การดึงแท็ก

รายการแท็กโพสต์ โดยเรียงลำดับตามชื่อและแสดงจำนวนโพสต์:

query {
  postTags(
    sort: { order: ASC, by: NAME }
    pagination: { limit: 50 }
  ) {
    id
    name
    url
    postCount
  }
}

แท็กทั้งหมดในโพสต์:

query {
  post(by: { id: 1 }) {
    tags {
      id
      name
      url
    }
  }
}

ชื่อแท็กในโพสต์:

query {
  posts {
    id
    title
    tagNames
  }
}

รายการแท็กที่กำหนดไว้ล่วงหน้า:

query {
  postTags(filter: { ids: [66, 70, 191] }) {
    id
    name
    url
  }
}

กรองแท็กตามชื่อ:

query {
  postTags(filter: { search: "oo" }) {
    id
    name
    url
  }
}

นับจำนวนผลลัพธ์แท็ก:

query {
  postTagCount(filter: { search: "oo" })
}

การแบ่งหน้าแท็ก:

query {
  postTags(
    pagination: {
      limit: 5,
      offset: 5
    }
  ) {
    id
    name
    url
  }
}

การดึงค่า meta:

query {
  postTags(
    pagination: { limit: 5 }
  ) {
    id
    name
    metaValue(
      key: "someKey"
    )
  }
}

การตั้งค่าแท็กในโพสต์

Mutation:

mutation {
  setTagsOnPost(
    input: {
      id: 1499, 
      tags: ["api", "development"]
    }
  ) {
    status
    errors {
      __typename
      ... on ErrorPayload {
        message
      }
    }
    postID
    post {
      tags {
        id
      }
      tagNames
    }
  }
}

Nested mutation:

mutation {
  post(by: { id: 1499 }) {
    setTags(
      input: {
        tags: ["api", "development"]
      }
    ) {
      status
      errors {
        __typename
        ... on ErrorPayload {
          message
        }
      }
      postID
      post {
        tags {
          id
        }
        tagNames
      }
    }
  }
}

การสร้าง อัปเดต และลบแท็กโพสต์

Query นี้สร้าง อัปเดต และลบ term ของแท็กโพสต์:

mutation CreateUpdateDeletePostTags {
  createPostTag(input: {
    name: "Some name"
    slug: "Some slug"
    description: "Some description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...PostTagData
    }
  }
 
  updatePostTag(input: {
    id: 1
    name: "Some updated name"
    slug: "Some updated slug"
    description: "Some updated description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...PostTagData
    }
  }
 
  deletePostTag(input: {
    id: 1
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
fragment PostTagData on PostTag {
  id
  name
  slug
  description
}