Custom Types Relationships

Hi, I would like to create a relationship or link between custom types. However, I would like that to be done in both sides. For instance, I have a custom type “category” and a custom type “article”
I created the content relationship in the custom type “article”

I have
Article 1: Category A and Category B
Article 2: Category A

However I’d like to have the following automatically updated
Category A: Article 1 and Article 2.

If articles get added or removed from a category then it will reflect in both the category and the articles

Is this something possible or in your roadmap?

Thank you!

Hi,
Welcome to Prismic community, I will try to help you with this.

In order to be able to help you with this, may I know what is the exact use-case of what you are trying to achieve?

Best,
Fares

Hi Fares,

Of course. Well, that is the exact user-case. Just to summarise it a little bit better:

I have the following custom types:

  • Article
  • Category
  • Place

In prismic, the content editors will assign categories and places to each article. So an article can have multiple categories and multiple places.

I’ve added a “Content relationship” in the Article Custom Type and select the Category Custom type so this one is linked.
Also, I’ve added another “Content relationship” in the Article custom type so it allows to select places.

Example:

Article 1 belongs to category A and category B
Article 2 belongs to category A
Article 1 is about place 1 and place 2

In Prismic dashboard:
When editing an article, you can select multiple categories and multiple places.
However, when editing a category you can’t really see what articles have been linked to the category.

Question: is there a feature to see the articles linked to the category?

In the front end, we will require:
case 1. A page for Article 1 that shows that it belongs to category A and category B
case 2. A page for Category A that shows a list of articles, in this example Article 1 and Article 2
case 3. A page for Place 1 that shows a list of articles it has been mentioned in. In this example, Article 1, and a list of Categories in this example Category A and Category B because they belong to the Article the place is linked to (Article 1)

Case 1 seems to be the easy one as the content relationship between category an article sits on the Article custom type.

Question: Can you please help me to try to achieve case 2 without having to loop in all the articles and checking if they have category A linked to them?

Case 3 is tricky so for now, I’ll focus on case 1 and case 2

Thanks!

Hi Fransy,

I apologize for the long delay, this issue hasn't been tracked correctly and we have already installed a workflow to avoid such an error in the future

If I understood correctly then here is how to do it:

  • Case 2:

You will need to do 2 queries to achieve that, When you are in the Category page you get the UID from the Url and you fetch your Category X by it's UID

Rest API:

[at(my.category.uid, "CATEGORY-X-UID")]

Result:

Using the category Id returned you do your second query to fetch the list of Articles such as:

Rest API:

q = [[at(my.catergory.article_link,"WNje3SUAAEGBu8bc")]]

and to get all the articles linked to this category you can query articles by a content relationship that has that category id such as:

[at(my.article.category_link,"X3b_1hIAACMA-YWk")]

JavaScript:

api.query(Prismic.Predicates.at('my.article.category_link', 'WNje3SUAAEGBu8bc')).then(function(response) {
  // response is the response object, response.results holds the documents
});

Result:

  • Case 3:
    Case 3 is almost the same as case 2 except that you add another query to get the linked places.

You can read more about querying by content relationship in the article

Hope that this answers your question,
Fares

This issue has been closed due to inactivity. Flag to reopen.