การ Query ข้อมูล WordPressหมวดหมู่โพสต์
หมวดหมู่โพสต์
ต่อไปนี้เป็นตัวอย่าง queries สำหรับดึงข้อมูลหมวดหมู่โพสต์
การดึงหมวดหมู่
รายการหมวดหมู่โพสต์ เรียงลำดับตามชื่อ และแสดงจำนวนโพสต์:
query {
postCategories(
sort: { order: ASC, by: NAME }
pagination: { limit: 50 }
) {
id
name
url
postCount
}
}หมวดหมู่ทั้งหมดในโพสต์:
query {
post(by: { id: 1 }) {
categories {
id
name
url
}
}
}ชื่อหมวดหมู่ในโพสต์:
query {
posts {
id
title
categoryNames
}
}รายการหมวดหมู่ที่กำหนดไว้ล่วงหน้า:
query {
postCategories(filter: { ids: [2, 5] }) {
id
name
url
}
}กรองหมวดหมู่ตามชื่อ:
query {
postCategories(filter: { search: "rr" }) {
id
name
url
}
}นับจำนวนผลลัพธ์หมวดหมู่:
query {
postCategoryCount(filter: { search: "rr" })
}แบ่งหน้าหมวดหมู่:
query {
postCategories(
pagination: {
limit: 3,
offset: 3
}
) {
id
name
url
}
}เฉพาะหมวดหมู่ระดับบนสุด และลูกระดับที่ 2:
{
postCategories(pagination: { limit: 50 }, filter: { parentID: 0 }) {
...CatProps
children {
...CatProps
children {
...CatProps
}
}
}
}
fragment CatProps on PostCategory {
id
name
parent {
id
name
}
childNames
childCount
}ดึงค่า meta:
query {
postCategories(
pagination: { limit: 5 }
) {
id
name
metaValue(
key: "someKey"
)
}
}การกำหนดหมวดหมู่ให้โพสต์
Mutation:
mutation {
setCategoriesOnPost(
input: {
id: 1499,
categoryIDs: [2, 5]
}
) {
status
errors {
__typename
... on ErrorPayload {
message
}
}
postID
post {
categories {
id
}
categoryNames
}
}
}Nested mutation:
mutation {
post(by: { id: 1499 }) {
setCategories(
input: {
categoryIDs: [2, 5]
}
) {
status
errors {
__typename
... on ErrorPayload {
message
}
}
postID
post {
categories {
id
}
categoryNames
}
}
}
}การสร้าง อัปเดต และลบหมวดหมู่โพสต์
Query นี้สร้าง อัปเดต และลบ term ของหมวดหมู่โพสต์:
mutation CreateUpdateDeletePostCategories {
createPostCategory(input: {
name: "Some name"
slug: "Some slug"
description: "Some description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
...PostCategoryData
}
}
updatePostCategory(input: {
id: 1
name: "Some updated name"
slug: "Some updated slug"
description: "Some updated description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
...PostCategoryData
}
}
deletePostCategory(input: {
id: 1
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
}
fragment PostCategoryData on PostCategory {
id
name
slug
description
parent {
id
}
}Prev
Next