Tagging and Filtering by Tag
First, let's cover how to create tags. In Content, create a Tag content type separate from your blog posts. It works the same way as Author. Then edit the Content model for your Blog Post and add a References field. This can point to a single tag or multiple tags.
Create a few tags in Content and link them to a post (the same way you'd select an Author). That alone is enough to have the post tagged.
All that's left is to filter by the target tag when displaying blog posts. As shown in the example below, the content you created will show up under fields, so you just need to specify the target ID there and it will return the matching content.
async fetch() {
const limit = 10
const skip = 0
const client = contentful.createClient(config)
const posts = await client.getEntries({
content_type: process.env.CTF_BLOG_POST_TYPE_ID,
limit,
skip,
order: '-sys.createdAt',
'fields.tags.sys.id': 'ID of the tag to filter by',
})
},
Reference Links to a specific item If you want to retrieve all items linked to a specific entry, the query URL should filter entries on their specific content_type, linking_field (field to link items) and entry_id from the target entry. Contentful Document links/links-to-a-specific-item
Exclusion
Say you want to pull other posts with the same tag as related articles for a given post. In that case, you'll likely want to exclude the current post itself from the related-articles list. For that, you can use [ne].
In the example below, we're fetching blog posts in order of newest first, excluding the post with a given slug.
async fetch() {
const limit = 5
const client = contentful.createClient(config)
const posts = await client.getEntries({
content_type: process.env.CTF_BLOG_POST_TYPE_ID,
limit,
order: '-sys.createdAt',
'fields.slug[ne]': 'slug',
})
},
Reference Inequality operator Uses the [ne] parameter to exclude items matching a certain pattern. Contentful Document search-parameters/inequality-operator
Full-Text Search
Contentful even provides full-text search out of the box! Just add query to getEntries and it will run a full-text search across your content. Talk about a complete feature set!
async fetch() {
const limit = 10
const skip = 0
const client = contentful.createClient(config)
const posts = await client.getEntries({
content_type: process.env.CTF_BLOG_POST_TYPE_ID,
limit,
skip,
order: '-sys.createdAt',
query: 'text to search for',
})
},
Reference Full-text search It's possible to perform a full-text search across all text and symbol fields with the query parameter. Note: Full-text search is case insensitive and might return more results than expected. A query will only take values with more than 1 character. Contentful Document search-parameters/full-text-search
Incidentally, it's also possible to search on specific fields. For example, if you want to search within article titles, it might look like this:
async fetch() {
const limit = 10
const skip = 0
const client = contentful.createClient(config)
const posts = await client.getEntries({
content_type: process.env.CTF_BLOG_POST_TYPE_ID,
limit,
skip,
order: '-sys.createdAt',
'fields.title[match]': 'Nuxt', // Search for posts of Content Type BLOG_POST whose title contains "Nuxt"
})
},
Reference Full-text search on a field You can perform a full-text search on a specific field with the [match] operator. Note: Full-text search is case insensitive and might return more results than expected. A query will only take values with more than 1 character. Contentful Document search-parameters/full-text-search-on-a-field
Summary
Reading through the documentation, you keep finding yourself thinking "oh, you can do that too? 👀" There's a ton to explore, and it would be fun to try building out an MVP using all these different features.
We'll share more useful features as we discover them going forward!