Query WordPress 数据
Query WordPress 数据文章分类

文章分类

以下是获取文章分类数据的 Query 示例。

获取分类

按名称排序并显示文章数量的文章分类列表:

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

仅顶级分类及第二层子分类:

{
  postCategories(pagination: { limit: 50 }, filter: { parentID: 0 }) {
    ...CatProps
    children {
      ...CatProps
      children {
        ...CatProps
      }
    }
  }
}
 
fragment CatProps on PostCategory {
  id
  name
  parent {
    id
    name
  }
  childNames
  childCount
}

获取元值:

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

嵌套 mutation:

mutation {
  post(by: { id: 1499 }) {
    setCategories(
      input: {
        categoryIDs: [2, 5]
      }
    ) {
      status
      errors {
        __typename
        ... on ErrorPayload {
          message
        }
      }
      postID
      post {
        categories {
          id
        }
        categoryNames
      }
    }
  }
}

创建、更新和删除文章分类

以下 Query 用于创建、更新和删除文章分类术语:

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