Zapytania o dane WordPress
Zapytania o dane WordPressKategorie postów

Kategorie postów

Oto przykłady queries do pobierania danych kategorii postów.

Pobieranie kategorii

Lista kategorii postów posortowana według nazwy z liczbą postów:

query {
  postCategories(
    sort: { order: ASC, by: NAME }
    pagination: { limit: 50 }
  ) {
    id
    name
    url
    postCount
  }
}

Wszystkie kategorie w poście:

query {
  post(by: { id: 1 }) {
    categories {
      id
      name
      url
    }
  }
}

Nazwy kategorii w postach:

query {
  posts {
    id
    title
    categoryNames
  }
}

Lista predefiniowanych kategorii:

query {
  postCategories(filter: { ids: [2, 5] }) {
    id
    name
    url
  }
}

Filtrowanie kategorii według nazwy:

query {
  postCategories(filter: { search: "rr" }) {
    id
    name
    url
  }
}

Zliczanie wyników kategorii:

query {
  postCategoryCount(filter: { search: "rr" })
}

Paginowanie kategorii:

query {
  postCategories(
  	pagination: {
  	  limit: 3,
  	  offset: 3
  	}
  ) {
    id
    name
    url
  }
}

Tylko kategorie najwyższego poziomu i 2. poziom kategorii podrzędnych:

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

Pobieranie wartości meta:

query {
  postCategories(
  	pagination: { limit: 5 }
  ) {
    id
    name
    metaValue(
      key: "someKey"
    )
  }
}

Przypisywanie kategorii do postu

Mutation:

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

Zagnieżdżona mutation:

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

Tworzenie, aktualizowanie i usuwanie kategorii postu

Ta query tworzy, aktualizuje i usuwa terminy kategorii postów:

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