Interrogare i dati di WordPress
Interrogare i dati di WordPressTag personalizzati

Tag personalizzati

Continua a leggere nella guida Lavorare con i tag personalizzati.

Questi sono esempi di queries per recuperare i dati delle tassonomie di tag personalizzati.

Tassonomie non mappate allo schema

Recuperare i tag con la tassonomia "product-tag":

query {
  tags(taxonomy: "product-tag") {
    __typename
 
    ...on Tag {
      count
      description
      id
      name
      slug
      url
    }
    
    ...on GenericTag {
      taxonomy   
      customPostCount
      customPosts {
        __typename
        ...on CustomPost {
          id
          title
        }
      }
    }
  }
}

Recuperare i tag associati a un custom post

Il tipo GenericCustomPost possiede il campo tags, per recuperare i tag personalizzati aggiunti al custom post:

query {
  customPosts(
    filter: { customPostTypes: "product" }
  ) {
    __typename
 
    ... on GenericCustomPost {
      tags(taxonomy: "product-tag") {
        __typename
        id
        name
        taxonomy
      }
    }
  }
}

Filtrare i custom post per tag

Per recuperare i custom post con determinati tag, possiamo usare l'input filter.tags:

query {
  customPostsByTagIDs: customPosts(
    filter: {
      tags: {
        includeBy: {
          ids: [26, 28],
        }
        taxonomy: "product-tag"
      }
    }
  ) {
    id
    title
  }
 
  customPostsByTagSlugs: customPosts(
    filter: {
      tags: {
        includeBy: {
          slugs: ["tango", "rock"]
        }
        taxonomy: "product-tag"
      }
    }
  ) {
    id
    title
  }
}

Impostare i tag su un custom post

Mutation:

mutation {
  setTagsOnCustomPost(
    input: {
      id: 1499, 
      tags: ["api", "development"]
      taxonomy: "tag-taxonomy"
    }
  ) {
    status
    errors {
      __typename
      ... on ErrorPayload {
        message
      }
    }
    customPostID
    customPost {
      tags(taxonomy: "tag-taxonomy") {
        id
      }
      tagNames(taxonomy: "tag-taxonomy")
    }
  }
}

Mutation annidata:

mutation {
  customPost(by: { id: 1499 }) {
    setTags(
      input: {
        tags: ["api", "development"]
        taxonomy: "tag-taxonomy"
      }
    ) {
      status
      errors {
        __typename
        ... on ErrorPayload {
          message
        }
      }
      customPostID
      customPost {
        tags(taxonomy: "tag-taxonomy") {
          id
        }
        tagNames(taxonomy: "tag-taxonomy")
      }
    }
  }
}

Creare, aggiornare ed eliminare un tag personalizzato

Questa query crea, aggiorna ed elimina i termini di tag per un tag personalizzato some-tag-taxonomy:

mutation CreateUpdateDeleteTags {
  createTag(input: {
    taxonomy: "some-tag-taxonomy",
    name: "Some name"
    slug: "Some slug"
    description: "Some description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...TagData
    }
  }
 
  updateTag(input: {
    id: 1
    taxonomy: "some-tag-taxonomy"
    name: "Some updated name"
    slug: "Some updated slug"
    description: "Some updated description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...TagData
    }
  }
 
  deleteTag(input: {
    id: 1
    taxonomy: "some-tag-taxonomy"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
fragment TagData on Tag {
  id
  name
  slug
  description
}