Interrogare i dati di WordPress
Interrogare i dati di WordPressCategorie del post

Categorie del post

Questi sono esempi di queries per recuperare dati sulle categorie dei post.

Recuperare le categorie

Elenco delle categorie dei post, ordinate per nome e con il conteggio dei post:

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

Tutte le categorie in un post:

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

Nomi delle categorie nei post:

query {
  posts {
    id
    title
    categoryNames
  }
}

Un elenco di categorie predefinite:

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

Filtrare le categorie per nome:

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

Contare i risultati delle categorie:

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

Paginare le categorie:

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

Solo le categorie di primo livello e il 2° livello di figlie:

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

Recuperare i valori meta:

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

Impostare le categorie su un post

Mutation:

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

Mutation annidata:

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

Creare, aggiornare ed eliminare una categoria di post

Questa query crea, aggiorna ed elimina i termini delle categorie dei post:

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