Get Fields from Records in a Reciprocal Relationship of a Related Post
- This topic has 6 replies, 2 voices, and was last updated 1 year, 1 month ago by
Peter.
-
AuthorPosts
-
March 8, 2024 at 8:06 AM #44771
saint
ParticipantHi, I have the following scenario setup:
1.) A Product CPT with the 4 widget records (e.g. red widget, yellow widget, green widget, purple widget.)
2.) A reciprocal relationship setup in the Red widget record with 3 other Product records (e.g. yellow, green, purple widgets)
3.) A Factories CPT with 1 record (e.g. Main widget factory)
I successfully created an MB View that displays the URL link back to the Red widget from the Main widget factory record.
I was also able to successfully display the records in the reciprocal relationship in the SAME CPT that it's inserted in.
PROBLEM: I've tried different ways, but I'm not sure how to pull back fields from the 3 records defined in the reciprocal relationship of the related post, and have it show up in the Factory CPT record that it's linked to.
{% set relationship = attribute( relationships, 'widget-cpt-to-factory-cpt' ) %} {% for post in relationship.from %} ANOTHER NESTED RELATIONSHIP IN HERE? {% endfor %}
I wasn't sure if I needed to create the above View, then a separate one to set this as the parent so that it is nested?
Thank you.
March 8, 2024 at 9:50 PM #44773Peter
ModeratorHello,
I understand you want to display: Main widget factory > Red widget related to Main widget > Yellow, Green widgets related to Red widget
on the same page. You can use two for loops to do that, please follow this topic https://support.metabox.io/topic/traversing-multiple-related-cpts/March 9, 2024 at 4:23 AM #44776saint
ParticipantHi Peter,
Thank you for your reply. I read through the link, plus the reference link in that thread and redid the logic.
For the most part, it looks like the nested views is working. Except when I got to the Reciprocal Relationship resultset.
After getting to that View, the records I pull back from that Relationship is the same title of the related record all 3 times, when I was expecting the titles of the 3 records selected in that Reciprocal Relationship.
Backend setup in the Reciprocal Relationship: https://share.zight.com/WnuXXDgD
MB View code: https://pastebin.com/aNY9TtWm
Frontend result: https://share.zight.com/KouKK16qCould I be missing something that's making it repeat itself?
March 11, 2024 at 9:19 PM #44800Peter
ModeratorHello,
Please share your site credentials via this contact form https://metabox.io/contact/
I will take a closer look.March 13, 2024 at 10:35 PM #44840Peter
ModeratorHello,
This happens because you are in the
software_reciprocal
loop but output thesoftware
post title. I fix that issue with this code{{ software_reciprocal.post_title }}
{% set args = { post_type: 'alternative' } %} {% set alternatives = mb.get_posts ( args ) %} {% for alternative in alternatives %} {# {{ alternative.post_title }} #} <h2>Alternative: {{ alternative.post_title }}</h2> {% set software_args = { post_type: 'software', relationship: {id: 'software-cpt-and-software-alternatives-cpt', to: alternative.ID} } %} {% set softwares = mb.get_posts ( software_args ) %} {% for software in softwares %} <h3>Software: {{ software.post_title }}</h3> {# {{ software.post_title }} #} {% set software_reciprocal_args = { post_type: 'software', relationship: {id: 'software-cpt-reciprocal-relationship', from:software.ID} } %} {% set software_reciprocals = mb.get_posts ( software_reciprocal_args ) %} {% for software_reciprocal in software_reciprocals %} <h4>Software_reciprocal: {{ software_reciprocal.post_title }}</h4> {% endfor %} {% endfor %} {% endfor %}
March 14, 2024 at 3:22 PM #44852saint
ParticipantThank you, Peter!
That worked perfectly. I was under the impression that I could just pull back from field name since it is looping through the same reciprocal CPT, but will make sure to check the loop name next time and output from there.
As an aside, in trying to make this query a little more efficient, and easier to execute through the UI, I tried out different combinations and got the below to work.
{% set relationship = attribute( relationships, 'software-cpt-and-software-alternatives-cpt' ) %} {% for post in relationship.from %} <h2>Software: {{ post.title }}</h2> {% set software_reciprocal_args = { post_type: 'software', relationship: {id: 'software-cpt-reciprocal-relationship', from:post.ID} } %} {% set software_reciprocals = mb.get_posts ( software_reciprocal_args ) %} {% for software_reciprocal in software_reciprocals %} <h4>Software_reciprocal: {{ software_reciprocal.post_title }}</h4> {% endfor %} {% endfor %}
But is there any way for me to use the Queries in the INSERT FIELDS button in lieu of the below:
{% set software_reciprocal_args = { post_type: 'software', relationship: {id: 'software-cpt-reciprocal-relationship', from:post.ID} } %} {% set software_reciprocals = mb.get_posts ( software_reciprocal_args ) %} {% for software_reciprocal in software_reciprocals %} <h4>Software_reciprocal: {{ software_reciprocal.post_title }}</h4> {% endfor %}
If possible, I'd like to get rid of having to manually setting up the second args statement to get to the final nested loop just so I don't get a chance to mess things up by entering typos or setting the from/to direction wrong, etc.
Thank you!
March 15, 2024 at 8:27 PM #44869Peter
ModeratorHello,
You are using the custom query, so you need to add the parameter manually. The fields in the Insert fields area only work with the main query. Please read more about the custom query and main query in the documentation
https://docs.metabox.io/extensions/mb-views/#insert-fields
https://docs.metabox.io/extensions/mb-views/#main-query
https://docs.metabox.io/extensions/mb-views/#custom-query -
AuthorPosts
- You must be logged in to reply to this topic.