⟵ Resources

Creating Content Types and Relations in Strapi: A Step-by-Step Guide

CMS

After setting up your first Strapi app with PostgreSQL, it’s time to dive deeper into creating and managing your content. In this blog post, we will walk through the process of creating Content Types (Articles and Categories), setting up relationships between them, and utilizing these relationships to organize your content effectively.

Step 1: Create the Category Content Type

To start, we’ll create a Category content type to help us categorise our articles. If you haven’t followed the blog on setting up the Strapi app, check out our previous blog to setting up your first strapi app with PostgreSQL.

Create Category Content Type by following these steps:

  1. Access the Content-Types Builder:

    • Open your Strapi admin panel (http://localhost:1337/admin).

    • Click on “Content-Types Builder” in the left sidebar.

  2. Create a New Collection Type:

    • Click the “Create new collection type” button.

    • Enter Category as the name for your content type and click “Continue”.

  3. Add Fields to the Category Content Type:

    • Click “Add another field” and select the following fields:

      • name: Text (Short text)

      • description: Text (Long text)

    • Click “Save” in the top right corner.

Strapi will automatically generate the necessary API endpoints for the Category content type.

Step 2: Create the Article Content Type

Next, we’ll create the Article content type, which will include a relationship with the Category content type.

Follow these steps:

  1. Create a New Collection Type:

    • Go back to the “Content-Types Builder”.

    • Click the “Create new collection type” button.

    • Enter Article as the name for your content type and click “Continue”.

  2. Add Fields to the Article Content Type:

    • Click “Add another field” and select the following fields:

      • Title: Text (Short text)

      • Description: Rich Text

      • Slug Text (Short text)

    • Click “Save” to add these fields.

  3. Add a Relationship Field:

    • While still editing the Article content type, click “Add another field”.

    • Select “Relation” from the field types.

    • Choose the relation type as ArticleCategory with a many-to-many relationship (i.e., articles has and belongs to many articles)

      add new relation field in Strapi

    • Click “Finish” and then “Save” in the top right corner.

Strapi will update the API endpoints to include this relationship.

Step 3: Add Categories

With the content types in place, let’s add some categories:

  1. Go to Content Manager:

    • Click on “Content Manager” in the left sidebar.

  2. Add New Categories:

    • Select Categories from the available content types.

    • Click “Add New Category”.

    • Fill in the Name and Description fields.

    • Click “Save” to add the category.

    • Repeat this process to add multiple categories as needed (e.g., Technology, Health, Travel).

 Step 4: Add Articles

Now, we’ll add some articles and assign them to categories:

  1. Go to Content Manager:

    • Select Articles from the available content types.

  2. Add New Articles:

    • Click “Add New Article”.

    • Fill in the Title, Description, and Slugfields.

    • Assign the article to a category by selecting a category from the dropdown list in the Category field.

    • Click “Save” to add the article.

    • Repeat this process to add multiple articles and assign them to different categories.

Step 5: Accessing Articles and Categories via the API

*Don’t forget to grant API permission for each content type that was created.

Settings > Roles below USERS & PERMISSIONS PLUGIN > Public > [your content-types]

With your content in place, you can now access it via Strapi’s RESTful API. Here are a few example endpoints:

Get all categories:

GET http://localhost:1337/api/categories

get all categories in Strapi

Get all articles:

GET http://localhost:1337/api/articles
get all articles in Strapi

Get articles in a specific category: You can filter articles by category using query parameters. For example:

GET http://localhost:1337/api/categories?fields[0]=name&populate[articles][fields][0]=Title
get articles in specific categori in Strapi

Conclusion

You’ve successfully created Content Types and set up relations in Strapi. This powerful relationship allows you to organize and manage your content more effectively. With Strapi’s flexible and user-friendly interface, managing complex content structures becomes a breeze.

Explore Strapi’s extensive documentation to learn more about advanced features and customization options. Stay tuned for more tutorials on how to leverage Strapi for your projects. Happy coding!

This article is written by Ken Le, a Sitefinity and .Net Core developer from Websparks.

Strapi is an open-source headless CMS.

Find out more about headless CMS.

Best Email Writing Tips for 2025

8 Irresistible Tips to Craft Ad Copy That Converts in Paid Online Campaigns

Sitefinity Cache Invalidation

Portfolios

Resources

Contact Us

Website Development Company in Singapore white logo
LowCarbonSG Logo
Terms & Conditions | Privacy Policy | Accessibility Statement

Apply Now!

Upload Resume (with a 2mb maximum file size)
Accepted file types: doc, docx, pdf, xps, Max. file size: 2 MB.
This field is for validation purposes and should be left unchanged.