Query WordPress 数据
Query WordPress 数据文章标签

文章标签

以下是获取文章标签数据的 Query 示例。

获取标签

按名称排序列出文章标签,并显示其文章数量:

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

获取元数据值:

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

嵌套 Mutation:

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

创建、更新和删除文章标签

此 Query 用于创建、更新和删除文章标签分类项:

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
}