WordPressデータのクエリ
WordPressデータのクエリ投稿カテゴリー

投稿カテゴリー

投稿カテゴリーデータを取得するクエリの例です。

カテゴリーの取得

投稿カテゴリーの一覧を名前順に並べ、投稿数を表示する:

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
}

メタ値の取得:

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

投稿カテゴリーの作成・更新・削除

このクエリは投稿カテゴリーのタームを作成、更新、および削除します:

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