Relationship not working.
Support › MB Relationships › Relationship not working.Resolved
- This topic has 6 replies, 2 voices, and was last updated 3 years, 11 months ago by
Long Nguyen.
-
AuthorPosts
-
May 10, 2021 at 2:13 PM #28064
Thibaut
ParticipantHello.
I have some relationships (reciprocal)
book-photographer and photographer-book
book-publisher and publisher-book1st one works when I extract the information with a view, I get the books from a photographer but for the 2nd, I do not get the books from a publisher unless I populate myself the books into the publisher post directly.
What am doing wrong?
May 10, 2021 at 10:02 PM #28073Long Nguyen
ModeratorHi,
Can you please share the code that creates the relationships and the code that get the books? I will help you to check the issue.
May 10, 2021 at 11:27 PM #28076Thibaut
Participantcode that create the relationnship (sorry not sure if this is what you mean by get the code, I just extracted the php function from the relationship in metabox)
<div> <?php add_action( 'mb_relationships_init', 'your_prefix_function_name' ); function your_prefix_function_name() { MB_Relationships_API::register( [ 'id' => 'publisher-book', 'reciprocal' => true, 'from' => [ 'object_type' => 'post', 'post_type' => 'publisher', 'meta_box' => [ 'priority' => 'high', 'style' => 'seamless', ], 'field' => [ 'name' => 'Publisher', ], ], 'to' => [ 'object_type' => 'post', 'post_type' => 'book', 'meta_box' => [ 'priority' => 'high', 'style' => 'seamless', ], 'field' => [ 'name' => 'Books', 'add_button' => 'Add books', ], ], ] ); } </div> code that get the books <div> <div class="container3"> {% set relationship = attribute( relationships, 'publisher-book' ) %} {% for post in relationship.to %} <div class="container4"> <a href="{{ post.url }}"> <img src="{{ post.thumbnail.medium.url }}" width="{{ post.thumbnail.medium.width }}" height="{{ post.thumbnail.medium.height }}" alt="{{ post.thumbnail.medium.alt }}" /> <h4>{{ post.title }}</h4></a> </div> {% endfor %} </div> </div>
thank you.
May 11, 2021 at 1:19 PM #28084Long Nguyen
ModeratorHi,
If you use
{% for post in relationship.to %}
that means it will retrievepublisher
posts which connected to the currentbook
. View location Book.If you use
{% for post in relationship.from %}
that means it will retrievebook
posts which connected to the currentpublisher
. View location Publisher.Get more details on the documentation https://docs.metabox.io/extensions/mb-relationships/#getting-connected-items.
May 11, 2021 at 1:49 PM #28085Thibaut
Participantif I use 'from' instead of 'to', it doesn't change anything.
May 11, 2021 at 1:57 PM #28086Thibaut
ParticipantI had to change publisher-book to book publisher also.
Now I am confused.
publisher-book with to is not the same as book-publisher with from?
May 12, 2021 at 9:08 AM #28118Long Nguyen
ModeratorHi,
Thanks for your additional information.
I will need more time to check on this case. On the other hand, you can use the PHP code to get connected post by using PHP, see more on the document https://docs.metabox.io/extensions/mb-relationships/#getting-connected-items.
Wrap it in a custom function
function my_custom_relationship() { $pages = MB_Relationships_API::get_connected( [ 'id' => 'posts_to_pages', 'from' => get_the_ID(), ] ); foreach ( $pages as $p ) { echo $p->post_title; } }
and call it in the View
{{ mb.my_custom_relationship() }}
-
AuthorPosts
- You must be logged in to reply to this topic.