Contentful logo

Contentful Community

Is there a way to filter content to get a full reference to many list from a content entry?

content_modeling
#1

I know that is a bit of a mouthful.

Say i have a category ‘fiction’, that has a reference to many, for different ‘books’. I create the category and reference many different books inside the category.

How do i go about making a filter view to grab only the ‘books’ in the reference list, inside the ‘fiction’ category entry?

I don’t know if i’m missing something as i’m new to contentful, but the only way I can get this to work is to make a reference to one entry from the ‘book’ to the ‘fiction’ category. Then i can get all books and filter from their reference. This seems a backwards way to do it though.

any help would be much appreciated.

0 Likes

#2

Hi @enddev.nz!

Which SDK are you using? Or are you calling the API directly? If using the JS SDK, you can simply get the ‘fiction’ category entry and all the referenced books will get retrieved at the same time. Go to this link to see this snippet:

const contentful = require('contentful')

const client = contentful.createClient({
  space: '<space_id>',
  environment: '<environment_id>', // defaults to 'master' if not set
  accessToken: '<content_delivery_api_key>'
})

client.getEntry('<entry_id>')
.then((entry) => console.log(entry))
.catch(console.error)

Using the Delivery API directly you’d request:

https://cdn.contentful.com/spaces/<space_id>/entries?sys.id=<fiction-entry-id>

with a header like this:

Authorization: Bearer <CDA-TOKEN>

And you’ll get a JSON response which will include the single category and all the books referenced from that category.

Hope this helps!

0 Likes

#3

sorry, I should of been more specific.

I was actually meaning when using the Contentful web interface, under the content section, when you can view all content.

0 Likes

#4

Why not click on the fiction category entry and just see all the books referenced there?

0 Likes

#5

yeah that is a way. I just wanted to see if it was possible to make a filter view for it. Thats alright.

Thanks for your replies

0 Likes

#6

If the model was reversed (books point to the fiction category) then you could make a saved view, but the model as you’ve described won’t work with a saved view.

0 Likes