Support Forum
Support › MB Relationships › Fields from related post(s) do not show in 'View' of Custom Post Type
Hi guys, I'm sorry to start off with such a negative post, but seriously your documentation for setting up relationships and displaying them in a view, is pretty much impossible to understand - either that or your software simply does not work.
I have wasted an entire weekend trying to setup what in Toolset is a very simple thing to do, but in Metabox is like trying to solve an impossible puzzle.
Even with your updated documentation I am no closer to being able to understand how to make things work. Please PLEASE improve your documentation for Relationships and Views - please PLEASE make some tutorial videos.
So, now on to what I am trying to achieve:
I have a custom post type called 'Sponsor' (think of Sponsor as an event, basically a sponsored event)
I then have another custom post type called 'Speaker'.
So, for each Sponsored event, I want to have one or more 'Speakers'. So in Metabox > Relationships I created a relationship called 'Sponsor Speaker' (Relationship ID 'sponsor-speaker', From Object Type 'Post', From Post Type 'Sponsor', To Object Type 'Post', To Post Type ''
I have Metabox AIO extension installed and so I am using Metabox builder and all the relevant plugins are installed and activated.
As some example code, I am loading all of my 'Sponsors' into my page, using a 'View', which has a setting type 'shortcode'. And so I am pasting in the shortcode to my Page. Here is the example code of my view:
{% set args = { post_type: 'sponsor', posts_per_page: -1 } %}
{% set posts = mb.get_posts( args ) %}
<div>
{% for post in posts %}
<div>
{{ post.title }}
{% set relationship = attribute( relationships, 'sponsor-speaker' ) %}
{% for post in relationship.to %}
{{ post.title }}
{% endfor %}
{% set relationship = attribute( relationships, 'sponsor-speaker' ) %}
{% for post in relationship.from %}
{{ post.title }}
{% endfor %}
</div>
{% endfor %}
</div>
The code for the relationship was taken from Metabox when I click 'Insert Field > Query > Then I get the option for 'sponsor-speaker' and I can select 'Connects To' or 'Connected From'. As you will see in my code I have tried clicking both of these (in case I was selecting the wrong piece), but my code above, all that I get in the front end is the post titles of my 'Sponsors', but nothing shows for the 'Speakers' (despite me having speakers connected in my Sponsor posts).
I can see from this thread = https://support.metabox.io/topic/relationship-doesnt-work-inside-a-for-loop-in-mb-view/ that I am not the only person who is having this problem. Also, please note that I have tried changing 'for post in relationship' to 'for post2 in relationship' and this does NOT work.
Please can someone urgently check if this is a bug, and if it is not a bug, then please can you tell me what is wrong with my code - please can I ask that you do not just post a link to your docs, because as I already said above, your docs are impossible for me to understand.
Thank you in advance for your help,
Keith
This paragraph:
So, for each Sponsored event, I want to have one or more 'Speakers'. So in Metabox > Relationships I created a relationship called 'Sponsor Speaker' (Relationship ID 'sponsor-speaker', From Object Type 'Post', From Post Type 'Sponsor', To Object Type 'Post', To Post Type ''
Should read:
So, for each Sponsored event, I want to have one or more 'Speakers'. So in Metabox > Relationships I created a relationship called 'Sponsor Speaker' (Relationship ID 'sponsor-speaker', From Object Type 'Post', From Post Type 'Sponsor', To Object Type 'Post', To Post Type 'Speaker'
Hi Keith,
It looks like the code relationship added from the Insert Field tab does not work with the custom query. You can create another query inside your current query, just like this
{% set args = { post_type: 'sponsor', posts_per_page: -1 } %}
{% set posts = mb.get_posts( args ) %}
<div>
{% for post in posts %}
<div>
Sponsor: {{ post.post_title }}
{% set args2 = { post_type: 'speaker', relationship: {
id: 'sponsor-speaker',
from: post.ID
} } %}
{% set posts2 = mb.get_posts( args2 ) %}
{% for post2 in posts2 %}
Related speaker: {{ post2.post_title }} <br>
{% endfor %}
</div>
{% endfor %}
</div>
Refer to this documentation https://docs.metabox.io/extensions/mb-views/#custom-query
I was looking to do the same thing and the code above, although adapted to my variables, doesn't work. Was anyone ever able to make this work? I have yet to find a post on this forum where someone says they've been able pull data from a relationship into a query.