Support Forum
Support › MB Relationships › Relationship working on Author page but not PostResolved
Hello,
Context:
I have a relationship set up from USER
to POST (PROVIDER)
and it's NOT set to reciprocal (though I tried checking it and it makes no difference to my issue).
I display the relationship using a shortcode made with MB Views. Here is the view:
{% set relationship = attribute( relationships, 'user-provider' ) %}
{% for post in relationship.to %}
<a href="{{ post.url }}">
<div class="mb-container related-provider provider-related-to-user">
<div class="provider-logo">
{% set field = attribute( post, '_thumbnail_id' ) %}
{% for item in field %}
<img src="{{ item.medium.url }}" alt="{{ item.medium.alt }}">
{% endfor %}
</div>
<div class="provider-name">
<span>{{ post.title }}</span>
</div>
</div>
</a>
{% endfor %}
And the shortcode is [mbv name="provider-related-to-user"] (as defined in the View)
Issue:
When I use the shortcode on the User/author page, it works correctly and the relationship is displayed for all connected Providers.
When I use the shortcode on a Blog post (I'm showing it under the author box), it doesn't display anything.
What's even more strange is that it used to show the relationship in the blog post -- though with an apparent bug, i.e. it showed only 1 connected Provider and not all of them, unlike on the author page where it shows them all. So I tried to play with the Relationship settings to see if it was the issue, it didn't help so I reverted it back to its initial settings, but now it doesn't display any relationship at all. I suspect something was set up wrong before anyway so I'm not feeling the need to understand why something was working before...
Basically, I'd like to show the user-provider relationship under the Author Box in my blog posts and I don't understand why it's not working.
Thank you in advance for your help
Hello,
For tracking the issue easier, I recommend using the function get_posts()
to get posts related to a user.
{% set args = { post_type: 'provider', relationship: {
id: 'user-provider',
from: userID
} } %}
{% set posts = mb.get_posts( args ) %}
{% for post in posts %}
...
{% endfor %}
The main issue here is you need to pass the correct userID to the arguments to get related posts. Please read more in the documentation https://docs.metabox.io/extensions/mb-relationships/#posts
https://docs.metabox.io/extensions/mb-views/#custom-query
Peter,
I appreciate your answer, unfortunately it's not working.
Would it be an issue with from: UserID
? I wonder if it should be something else since I need providers connected to the post author, not the current logged in user visiting the page.
Here is the View I created with your input:
{% set args = { post_type: 'licensee', relationship: {
id: 'user-provider',
from: userID
} } %}
{% set posts = mb.get_posts( args ) %}
{% for post in posts %}
<a href="{{ post.url }}">
<div class="mb-container related-provider provider-related-to-user">
<div class="provider-logo">
{% set field = attribute( post, '_thumbnail_id' ) %}
{% for item in field %}
<img src="{{ item.medium.url }}" alt="{{ item.medium.alt }}">
{% endfor %}
</div>
<div class="provider-name">
<span>{{ post.title }}</span>
</div>
</div>
</a>
{% endfor %}
Edit: you'll see post-type declared as licensee
above instead of provider
. Ignore that - I was trying it because I use a slug rewrite. But the result is the same with both.
Hello,
userID
should be a numeric value, I leave the text for reading. You should replace it with the user ID or author of the given post. For example:
from: 123
If you don't know how to get the post author, please read more here https://stackoverflow.com/questions/19176691/get-author-id-of-a-post-in-wordpress
If you are not able to complete the task, please try to create a customization service request here https://metabox.io/contact/
Our development team will help you with an extra fee.
Views already offers the {{ author.ID }}
variable, why can't I use that? I tried it and it makes the whole page break.
That said, to verify the code is working in the first place I replaced UserID
with the value 7
(user ID of the author of a post I'm testing the View on) and it's still not showing anything...
Are you sure the code you provided is sound?
Hello,
Can you please share some screenshots of the relationship setup between user and provider post?
Sure thing, in fact I can paste the generated PHP code for the relationship:
<?php
add_action( 'mb_relationships_init', 'your_prefix_function_name' );
function your_prefix_function_name() {
MB_Relationships_API::register( [
'id' => 'user-provider',
'from' => [
'object_type' => 'user',
'post_type' => 'licensee',
'taxonomy' => 'category',
'empty_message' => 'Not affiliated with a Provider',
'admin_column' => [
'position' => 'after title',
'title' => 'Affiliation',
'link' => 'view',
],
'meta_box' => [
'title' => 'Provider you\'re affiliated with',
'context' => 'normal',
'style' => 'seamless',
],
],
'to' => [
'object_type' => 'post',
'post_type' => 'licensee',
'admin_column' => [
'position' => 'after title',
'title' => 'Affiliated',
'link' => 'view',
],
'meta_box' => [
'title' => 'User accounts associated with this licensee',
'context' => 'normal',
'priority' => 'high',
],
'field' => [
'name' => 'Affiliated Licensee',
],
],
] );
}
But if you want a screenshot from the UI here is one https://i.imgur.com/vYJ3QdK.png
Hello,
I do not see any issue with that relationship settings. Please share your site credentials via this contact form https://metabox.io/contact/ and let me know where I can see the issue. I will take a closer look.
Thank you Peter, I just submitted a message with the login credentials
Hello,
I've just changed the post type in the arguments to licensee
{% set args = { post_type: 'licensee', relationship: {
id: 'user-provider',
from: 7
} } %}
and the related posts display in the author box as well, please check this screenshot https://monosnap.com/file/b4f5bIOJmtVDjkFLgJDbgnJ13ujRAl
Ah gotcha - I got tricked by my own slug rewrite. Thank you Peter!!
Oh, I rejoiced a bit too fast unfortunately. The View was set up for testing with the user ID "7", but of course I need a variable. I tried {{ author.ID }} but it still breaks the page...
If I just enter a User ID like 7
it works, but if I place the {{ author.ID }}
variable it breaks the page. Does the {{ author.ID }}
variable return anything else than the actual author user ID?
Hello,
The variable author.ID
does not display any value because the main query here is the post query, not the author query. You can follow the suggestion above to get the author ID of a post https://support.metabox.io/topic/relationship-working-on-author-page-but-not-post/?swcfpc=1#post-41528
and read more about the main query here https://docs.metabox.io/extensions/mb-views/#main-query