Hello,
I have been trying for a few days now to query all the documents 1) of a specific custom type 2) with a specific value at data.mainTag.
Scenario:
I want to create a slice called "SimilarMainTag" that will display all the pages with the same MainTag value as the one currently displayed.
So in my custom type [uid].js (custom type=pillar_page), in my getStaticProps function, I am retrieving the page.data.maintag to compare it with all data.maintag of documents of type "pillar_page":
export async function getStaticProps({ params, previewData }) {
const client = createClient({ previewData })
const page = await client.getByUID('pillar_page', params.uid)
// Filtering all pillar pages with the same MainTag
// Current page's mainTag - OK
const currentMainTag = page.data.maintag
// All pillar pages - OK
const pillarPages = await client.getAllByType('pillar_page')
// **All pillar pages with the same mainTag:
//Attempt 1
// const filteredPillarPages = await client.query(
// prismic.predicate.at('my.pillar_page.maintag', currentMainTag)
// )
//Attempt 2
// const filteredDocuments = await client.get({
// predicates: prismic.predicate.at('my.pillar_page.main_tag', page.id)
// })
// console.log('filtered docs :', filteredDocuments)
// From Prismic's documentation:https://prismic.io/docs/content-relationship#query-by-content-relationship
// const filteredDocuments = await client.get({
// predicates: prismic.predicate.at(
// 'my.example_custom_type.example_content_relationship',
// 'YesUgRIAACEA-UZD'
// )
// })
return {
props: {
metaTitle: page.data.meta_title,
metaDescription: page.data.meta_description,
ogImage: page.data.og_image.url,
page: page,
title: page.data.title,
upperTitle: page.data.uppertitle
}
}
}
export async function getStaticPaths() {
const client = createClient()
const pages = await client.getAllByType('pillar_page')
return {
paths: pages.map((page) => prismicH.asLink(page)),
fallback: false
}
}
Attempt 1 & 2 fall.
I am not sure neither how to import "prismic". Am I supposed to use prismic.H or import Prismic from 'prismic-javascript'? none of them worked so far.
Would you be as kind as telling me what am I doing wrong?
I think this kind of filtering should interest a lot of people for SEO purposes.
Best,