Relationship working on Author page but not Post
Support › MB Relationships › Relationship working on Author page but not PostResolved
- This topic has 16 replies, 2 voices, and was last updated 1 year, 11 months ago by
Olivier.
-
AuthorPosts
-
April 16, 2023 at 4:05 AM #41505
Olivier
ParticipantHello,
Context:
I have a relationship set up fromUSER
toPOST (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
April 17, 2023 at 8:53 PM #41516Peter
ModeratorHello,
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-queryApril 18, 2023 at 12:55 AM #41520Olivier
ParticipantPeter,
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 %}
April 18, 2023 at 12:57 AM #41521Olivier
ParticipantEdit: you'll see post-type declared as
licensee
above instead ofprovider
. Ignore that - I was trying it because I use a slug rewrite. But the result is the same with both.April 18, 2023 at 6:58 PM #41528Peter
ModeratorHello,
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.April 19, 2023 at 4:22 AM #41532Olivier
ParticipantViews 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 value7
(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?
April 19, 2023 at 5:38 PM #41539Peter
ModeratorHello,
Can you please share some screenshots of the relationship setup between user and provider post?
April 20, 2023 at 4:21 AM #41548Olivier
ParticipantSure 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
April 20, 2023 at 11:34 PM #41570Peter
ModeratorHello,
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.
April 27, 2023 at 2:46 PM #41633Olivier
ParticipantThank you Peter, I just submitted a message with the login credentials
April 27, 2023 at 11:35 PM #41638Peter
ModeratorHello,
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
April 28, 2023 at 6:02 AM #41641Olivier
ParticipantAh gotcha - I got tricked by my own slug rewrite. Thank you Peter!!
April 28, 2023 at 6:07 AM #41642Olivier
ParticipantOh, 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...
April 28, 2023 at 6:11 AM #41643Olivier
ParticipantIf 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?April 29, 2023 at 11:57 PM #41659Peter
ModeratorHello,
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-41528and read more about the main query here https://docs.metabox.io/extensions/mb-views/#main-query
-
AuthorPosts
- You must be logged in to reply to this topic.