Dear Sir/Madam,
Please refer to the attached screenshot, a student can have many classes and each class is related to only one course, the course contains many materials and the class also contain its materials.
How can I show all materials that belong to the class and also to the course when a student visits a class? Could you please let me know how I can do using the Views and API code?
Best regards,
Kelvin.
Hello,
In your case, you can try these:
1) Setup four post types:
- students
- classes
- courses
- materials
2) Setup three relationships:
- Many-to-many relationship between "students" and "classes"
- One-to-one relationship between "classes" and "courses"
- One-to-many relationship between "courses" and "materials"
In the single "classes" posts, you can display a nested view:
a) Parent post view "related courses post":
- Query courses posts
- Filter by relationship between "classes" and "courses"
- In view's loop, display below child post view
b) Child post view "related materials posts":
- Query materials posts
- Filter by relationship between "classes" and "courses"
- In view's loop, display below materials post information
More help:
https://toolset.com/course-lesson/displaying-related-posts/#displaying-many-related-items
If you are going to use custom PHP codes, please check our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
Dear Luo Yang,
Thanks for your reply, I have a question why it is recommended to have the student post type? I create the student role and assign the role to the WP user, is that ok?
Regarding to One-to-one relationship between "classes" and "courses", basically one course can be reused to many classes and one class can only be associated to one course, why you recommend to have one-to-one relationship between "classes" and "courses"?
Best regards,
Kelvin
I suggested it since you mentioned these:
each class is related to only one course
Then you can change it to:
One-to-many relationship between "courses" and "classes"
It depends on yourself, my suggestions are just for your reference.