Truy vấn dữ liệu WordPressTag tùy chỉnh
Tag tùy chỉnh
Đọc thêm trong hướng dẫn Làm việc với Tag tùy chỉnh.
Đây là các ví dụ về queries để lấy dữ liệu phân loại tag tùy chỉnh.
Phân loại chưa được ánh xạ vào schema
Lấy các tag với phân loại "product-tag":
query {
tags(taxonomy: "product-tag") {
__typename
...on Tag {
count
description
id
name
slug
url
}
...on GenericTag {
taxonomy
customPostCount
customPosts {
__typename
...on CustomPost {
id
title
}
}
}
}
}Lấy các tag liên kết với một custom post
Kiểu GenericCustomPost có trường tags, để lấy các tag tùy chỉnh đã thêm vào custom post:
query {
customPosts(
filter: { customPostTypes: "product" }
) {
__typename
... on GenericCustomPost {
tags(taxonomy: "product-tag") {
__typename
id
name
taxonomy
}
}
}
}Lọc custom post theo tag
Để lấy các custom post có tag nhất định, chúng ta có thể dùng input filter.tags:
query {
customPostsByTagIDs: customPosts(
filter: {
tags: {
includeBy: {
ids: [26, 28],
}
taxonomy: "product-tag"
}
}
) {
id
title
}
customPostsByTagSlugs: customPosts(
filter: {
tags: {
includeBy: {
slugs: ["tango", "rock"]
}
taxonomy: "product-tag"
}
}
) {
id
title
}
}Gán tag cho một custom post
Mutation:
mutation {
setTagsOnCustomPost(
input: {
id: 1499,
tags: ["api", "development"]
taxonomy: "tag-taxonomy"
}
) {
status
errors {
__typename
... on ErrorPayload {
message
}
}
customPostID
customPost {
tags(taxonomy: "tag-taxonomy") {
id
}
tagNames(taxonomy: "tag-taxonomy")
}
}
}Mutation lồng nhau:
mutation {
customPost(by: { id: 1499 }) {
setTags(
input: {
tags: ["api", "development"]
taxonomy: "tag-taxonomy"
}
) {
status
errors {
__typename
... on ErrorPayload {
message
}
}
customPostID
customPost {
tags(taxonomy: "tag-taxonomy") {
id
}
tagNames(taxonomy: "tag-taxonomy")
}
}
}
}Tạo, cập nhật và xóa một tag tùy chỉnh
Query này tạo, cập nhật và xóa các term tag cho một tag tùy chỉnh some-tag-taxonomy:
mutation CreateUpdateDeleteTags {
createTag(input: {
taxonomy: "some-tag-taxonomy",
name: "Some name"
slug: "Some slug"
description: "Some description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
...TagData
}
}
updateTag(input: {
id: 1
taxonomy: "some-tag-taxonomy"
name: "Some updated name"
slug: "Some updated slug"
description: "Some updated description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
...TagData
}
}
deleteTag(input: {
id: 1
taxonomy: "some-tag-taxonomy"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
}
fragment TagData on Tag {
id
name
slug
description
}