Support Forum
Support › MB Relationships › Relationship not working.Resolved
Hello.
I have some relationships (reciprocal)
book-photographer and photographer-book
book-publisher and publisher-book
1st 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?
Hi,
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.
code 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.
Hi,
If you use {% for post in relationship.to %}
that means it will retrieve publisher
posts which connected to the current book
. View location Book.
If you use {% for post in relationship.from %}
that means it will retrieve book
posts which connected to the current publisher
. View location Publisher.
Get more details on the documentation https://docs.metabox.io/extensions/mb-relationships/#getting-connected-items.
if I use 'from' instead of 'to', it doesn't change anything.
I 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?
Hi,
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() }}