Filtering a boolean fields in group field

Hello

I have a content-type with a group field who contains text and boolean fields. In my filter query i want only fields who are set to true.

Content-type ID: header
Group field ID : share_social_media
Boolean field ID : display_social_network

Code :

import type {PrismicPlugin} from "@prismicio/vue";
import type {HeaderDocument, PrismicDocumentWithoutUID} from "@prismicio/types";

export const useSocialShareMedia = () => {
    const prismic: PrismicPlugin = usePrismic();
    return useAsyncData(
        '$shareSocialMedia',
        async (): Promise<PrismicDocumentWithoutUID> => await prismic.client.getSingle<HeaderDocument>('header', {
            lang: 'fr-fr',
            fetch: 'my.header.share_social_media',
            filters: [
                prismic.filter.at('my.header.share_social_media.display_social_network', true),
            ]
        })
    ).data.value?.data.share_social_media;

But however i set true or false in back-office, all fields are displayed. So is my filter query wrong or i have to filter the response ?
Thank you.

Hi Stéphane,

I think there's a misunderstanding of what the filter option is for. It is used to filter document results. Filters check every document and compare a specified property on that document against the provided value. If the document passes the comparison, the API will include the document in its results. So if you have 20 pages, you can return only the documents with the tags "Macaron" and "Cupcake":

[at(document.tags, ["Macaron", "Cupcake"])]

Your document here is a single type, so only one document is ever returned.

You can't use the filter option to filter out fields returned from the API. You'll need to filter on the client side to only show fields with the boolean true on the front end.

Let me know if you need anything else.

Thanks.

I understand my confusion. Thank you very much, it's clear now :)

1 Like