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
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
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
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
});