Getting tags for all posts

Is there an easy way to fetch all of the tags for a document type? In my case, I have a blog with various different document tags on each post, and I would like to find a way to fetch all of those tags for all of the posts to make a category selection system.

I am using Nuxt for reference.

Thank you!

Hello Kolby!

If you’re using Prismic’s default tagging system you can make a ‘Query by multiple tags’. This query will retrieve all documents that match the given tags.

And to be able to get all the existing tags inside of your repo you just need to save the initial response of your endpoint, e.g https://your-repo-name.cdn.prismic.io/api/v2 (to get the correct endpoint for your project go to your repository settings / API & Security and select your technology to find the right endpoint), this response holds important metadata info about your repository, like all the Custom type names, all languages and of course all tags.

If you’re using a Custom tagging system, first you need to Query all the tags in your system and then Query all the documents linked to any given tag

Hi Paulina,

Thank you for the quick response. Unfortunately, I do not believe those solutions will work for me. I am using Prismic’s default tagging system, however I cannot query for a specific tag because I do not know what it is yet. The client can enter virtually any type of tag for a document, and I need to somehow get all of the tags in the system.

I can provide a specific example:
I have 3 documents, each with the type of post. Each post is tagged with the following, however in Nuxt I do not know this until I fetch the documents as a whole.

  • Post #1: Travel Food
  • Post #2: Beauty Travel
  • Post #3: Food Beauty Advice

In the above example, an easy solution would be to query the articles, and then parse the tags from them to create a list of tags. However, in my situation, there can be hundreds of articles, with virtually any tag, so it does not make sense to query the articles for the tags. I am wondering if there is a way to query the tags separately.

Hopefully that all made sense.

Thank you!

Yes, you can get all the tags that exist inside your repository, separately. As mentioned before, you can query the API, without any predicates, just the response from the endpoint itself will give you access to all existing tags.

See this example, here, before querying my Blog home singleton as normal, I’m saving the tags in a variable

 async asyncData({ $prismic, error }) {
try {
  const tags = $prismic.api.tags;
  const homepageContent = (await $prismic.api.getSingle("blog_home")).data;
  
  ...
 }

The $prismic.api response holds many things, as you can see, including all tags:

1 Like

This is fantastic, and exactly what I was looking for!

Thank you Paulina!