How to get the ID of the "to" object ?
Support › MB Relationships › How to get the ID of the "to" object ?Resolved
- This topic has 9 replies, 2 voices, and was last updated 2 years, 1 month ago by
[email protected].
-
AuthorPosts
-
March 2, 2023 at 10:21 PM #40794
[email protected]
ParticipantHi,
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 }}
March 3, 2023 at 7:14 AM #40799Peter
ModeratorHello,
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 } }
March 3, 2023 at 3:13 PM #40802[email protected]
ParticipantHello,
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 }}March 3, 2023 at 3:49 PM #40803[email protected]
ParticipantI 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.
March 3, 2023 at 6:16 PM #40805Peter
ModeratorHello,
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 %}
March 3, 2023 at 6:38 PM #40808[email protected]
ParticipantYes I tried this but it doesn't work either...
Nothing displayed with your code...March 3, 2023 at 9:16 PM #40813Peter
ModeratorHello,
Please share your site credentials via this contact form https://metabox.io/contact/
I will take a closer look.March 3, 2023 at 9:34 PM #40816[email protected]
ParticipantThx a lot for your help Peter,
I just sent the email with my credentials.
BR
OliverMarch 4, 2023 at 8:38 AM #40829Peter
ModeratorHello,
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
March 4, 2023 at 3:47 PM #40835[email protected]
ParticipantHi Peter,
Fantastic !
Thx a lot for your help and the issue 😉
It's perfect. -
AuthorPosts
- You must be logged in to reply to this topic.