Viewing relationships - how to get it showing??
- This topic has 4 replies, 2 voices, and was last updated 9 months ago by
EddyPiV.
-
AuthorPosts
-
August 1, 2024 at 1:52 PM #46071
EddyPiV
ParticipantHi,
I see many threads of MB users not succeeding in showing results from relationships, and I'm joining the group.
I have a relationship from students to courses (as I want to show students the courses he's (been) enrolled in).
In the database there is so far just 1 student with 2 courses. I'm logged in as that student.
I keep the code really basic, just to make sure that the logic of getting the courses for this student works. Next step will be to extend the info that will be shown.
Inspired by the example on https://metabox.io/mb-views-how-to-display-relationships/
{% set args = {post_type: 'courses', nopaging: true, relationship: {id: 'student-results', from: user.ID}} %} {% set courses = mb.get_posts( args ) %} {% for course in courses %} Yes - {{ course.post_ID }} {% endfor %}
Perhaps even course.post_ID is not correct, but at least I should see 2 lines of Yes. But nothing is shown.
Then I tried different code inspired on https://docs.metabox.io/extensions/mb-views/#relationship-fields
{% set relationship = attribute( relationships, 'student-results' ) %} {% for user in relationship.from %} Yes - {{ user.post_ID }} {% endfor %}
Again, nothing is shown.
I'm really struggling with this, and have spent lots of time already in getting it working.
Pls advise.
Thanks.August 1, 2024 at 9:23 PM #46077Peter
ModeratorHello Eddy,
Please share 2 full screenshots: relationship settings and View template. I will help you check the issue.
August 2, 2024 at 1:12 PM #46086EddyPiV
ParticipantThanks Peter.
I noticed that in the first attempt I had set the post type to courses, instead of course. That was it.
I expanded the code to show thumbnail and post title. That worked nicely.
Then I added the condition to show only for coursecategory = training, that's where I step into a critical error.{% set args = {post_type: 'course', nopaging: true, relationship: {id: 'student-results', from: user.ID}} %} {% set courses = mb.get_posts( args ) %} <div class="completed_courses"> {% for course in courses %} {% set coursecategory = mb.get_the_term_list( result.ID, 'coursecategory', '', ', ' ) %} {% if coursecategory is 'training' %} <div class="course"> {% set post = mb.get_post( result.ID ) %} {{ mb.get_the_post_thumbnail( result.ID, 'thumbnail' ) }} <p style="text-align: center;"><strong>{{ post.post_title }}</strong></p> </div> {% endif %} {% endfor %} </div>
Hopefully you can help me here?
Thanks!August 2, 2024 at 9:45 PM #46091Peter
ModeratorHello,
You can change the operator from
is
to==
to compare the value. The functionget_the_term_list()
returns a list of terms so it might not work if you compare to only one term.
Also, the variableresult
is not defined anywhere in your code. Please check this again.August 5, 2024 at 2:02 AM #46093EddyPiV
ParticipantThanks Peter, you pointed me in the right direction. It's working now.
-
AuthorPosts
- You must be logged in to reply to this topic.