Support Forum
Support › MB Relationships › How to get the ID of the "to" object ?Resolved
Hi,
I created a relationship called "Publication to Flipbook" (ID : publicationtoflip) between two custom post type : publication and 3d-flip-book.
Now, I want to display the ID of the to object in a view.
This is my code but it returns the ID of my custom post type Publication and not 3d-flip-book.
Could you help me please ?
Thx a lot !
{% set args = {
post_type: '3d-flip-book',
relationship: {id: 'publicationtoflip', to:post.ID }
}
%}
{% set posts = mb.get_posts( args ) %}
{{ post.ID }}
Hello,
If the view template is assigned to the Publication post type, and you want to display a list of 3d-flip-book posts, you can use the direction from
.
{% set args = {
post_type: '3d-flip-book',
relationship: { id: 'publicationtoflip', from: post.ID }
}
Hello,
Thx for your answer but I also tried with from instead of to and it doesn't word.
The code below displays the Id of the Publication post type and not 3d-flip-book post type.
I confirm, I have two Post types (publication and 3d-flip-book), a relationship (publicationtoflip, from 3d-flip-book to publication) and a view template is assigned to the Publication post type.
I would like to retrieve the ID of 3d-flip-book not the ID of Publication.However, with this code, I get the ID of publication, unfortunatly.
{% set args = {
post_type: '3d-flip-book',
relationship: { id: 'publicationtoflip', from: post.ID }
}
%}
{% set posts = mb.get_posts( args ) %}
{{ post.ID }}
I don't know if this can help but in another ticket, I noticed that "Each relationship meta box has an ID: {$relationship_id}_relationships_from and {$relationship_id}_relationships_to".
So, I suppose that there is a way to translate these IDs in the Twig language and use them in the form shortcode to show it on the front end.
Thx for your help.
Hello,
Did you use the for loop to iterate through the post array as on the documentation https://docs.metabox.io/extensions/mb-views/#custom-query
or you just use this code {{ post.ID }}
to output the post ID?
It should look like this
{% set args = {
post_type: '3d-flip-book',
relationship: { id: 'publicationtoflip', to: post.ID }
}
%}
{% set posts = mb.get_posts( args ) %}
{% for post in posts %}
Post title: {{ post.post_title }}
{% endfor %}
Yes I tried this but it doesn't work either...
Nothing displayed with your code...
Hello,
Please share your site credentials via this contact form https://metabox.io/contact/
I will take a closer look.
Thx a lot for your help Peter,
I just sent the email with my credentials.
BR
Oliver
Hello,
Can you please recheck the code on your site and see if it works?
{% set args = {
post_type: '3d-flip-book',
relationship: { id: 'publicationtoflip', to: post.ID }
}
%}
{% set my_posts = mb.get_posts( args ) %}
{% for post in my_posts %}
{{ post.ID }}
{% endfor %}
The issue here is you are enabling the option Reciprocal in the relationship. That means there is only one connection from 3d-flip-book to publication and not vice versa. So you cannot query to get the 3d-flip-book post ID from publication post. I've turned this option off then now it works correctly.
Please read more on the documentation https://docs.metabox.io/extensions/mb-relationships/#relationship-settings
Hi Peter,
Fantastic !
Thx a lot for your help and the issue 😉
It's perfect.