Trouble with post to user relationship
Support › MB Relationships › Trouble with post to user relationshipResolved
- This topic has 6 replies, 2 voices, and was last updated 3 years, 7 months ago by
Joseph Wong.
-
AuthorPosts
-
September 12, 2021 at 1:58 AM #30708
Joseph Wong
ParticipantHi,
I have created a relationship between custom post type (from) and user (to).
On the CPT page I see a drop down to select a user. However, on the user profile page I also see the selected user instead of the CPT post name. Is this the expected behavior?
From reading the documents, it seems reciprocal relationships can only be for the same type (e.g. post to post or user to user). However, I read a support thread here where the OP described creating a reciprocal relationship between post and user - so I'm confused.
Finally...
For retrieving connected data, I'm able to get the username using the post ID in the following code:
$users = get_users( [ 'relationship' => [ 'id' => 'client_to_assignment', 'from' => get_post('123'), // You can pass object ID or full object ], ] ); foreach ( $users as $user ) { echo 'relationship: ' . $user->display_name . '<br />'; }
However, I can't get the post ID/title, etc. using the user ID in the following code. It simply has no output:
$connected = new WP_Query( [ 'relationship' => [ 'id' => 'client_to_assignment', 'to' => get_current_user_id(), // You can pass object ID or full object ], 'nopaging' => true, ] ); while ( $connected->have_posts() ) : $connected->the_post(); echo $conntected->post_id; ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endwhile;
Please help. Thanks so much!
September 12, 2021 at 12:27 PM #30710Long Nguyen
ModeratorHi,
Can you please share the code that creates the relationship
client_to_assignment
? I've also replied to your question in the ticket system. Here is a copy of it:The reciprocal setting should be used for the same object type (post to post, user to user, term to term). What does that mean?
If you do not enable the reciprocal, for example, there are two relationships box display on the user page, Connect To and Connect From itself. Like this screenshot https://imgur.com/MHp7smp
If you enable the reciprocal, there is only one relationship box to select the user connected.And if this setting is enabled for the connection with the different object types. The relationship box to show the connection only show on the from object.
September 13, 2021 at 11:57 PM #30737Joseph Wong
ParticipantMorning, Long.
Thanks so much for getting back to me so fast! I really appreciate you.
I just replied to your email.
I created the relationship using the GUI instead of code. I did take a short video just now to show the Relationship page so you can see how I created it. The link is in my email reply.
Thanks again for your help!
Joseph
September 14, 2021 at 10:27 PM #30763Long Nguyen
ModeratorHi,
On the user profile page, I see a box to select the
client
post, so this relationship works as expected. There is another box to selectuser
, it might come from another relationship or other codes/plugins.Then, to get users connected to a post, you need to pass the post ID to the args.
$users = get_users( [ 'relationship' => [ 'id' => 'client_to_assignment', 'from' => 123, // You can pass object ID or full object ], ] );
and get posts connected to a user, but make sure that the user has posts connected.
$connected = new WP_Query( [ 'relationship' => [ 'id' => 'client_to_assignment', 'to' => get_current_user_id(), // You can pass object ID or full object ], 'nopaging' => true, ] );
September 15, 2021 at 8:32 AM #30771Joseph Wong
ParticipantHi Long,
Thank you for your help.
You're correct that the select user is a custom field I added to the user. The other one, select post, is the relationship field
I used the same code as the one you showed me for the top half of my code but it won't show me the posts connected to the user, for some reason. I found this in the Metabox documentation but I must have messed it up somehow.
Would you mind please take a look and tell me what I did wrong?
$connected = new WP_Query( [ 'relationship' => [ 'id' => 'client_to_assignment', 'to' => get_current_user_id(), // You can pass object ID or full object ], 'nopaging' => true, ] ); while ( $connected->have_posts() ) : $connected->the_post(); echo $connected->post_id; ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endwhile;
Not sure if this helps; here's what's in the relationship table in the database:
from to type order_from order_to 900 1 client_to_assignment 1 0
Thanks for your help!
September 15, 2021 at 10:37 PM #30783Long Nguyen
ModeratorHi,
I do not see any issue with your code on my end. Where did you add the code? Is it in a template of the theme?
You can try to add the argument
post_type
to the query and use this functionwp_reset_postdata
to reset query data. https://developer.wordpress.org/reference/functions/wp_reset_postdata/$connected = new WP_Query( [ 'post_type' => 'client', 'relationship' => [ 'id' => 'client_to_assignment', 'to' => get_current_user_id(), // You can pass object ID or full object ], 'nopaging' => true, ] ); while ( $connected->have_posts() ) : $connected->the_post(); echo $connected->post->ID; ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endwhile; wp_reset_postdata();
September 16, 2021 at 1:43 AM #30785Joseph Wong
ParticipantHi Long,
That did it!
After I added the post_type argument, I was able to get the related post.
The code is added on the actual page that displays the data. I'm using Oxygen builder.
Thanks again and hope you'll have a great rest of the week.
-
AuthorPosts
- You must be logged in to reply to this topic.