Trouble with post to user relationship

Support MB Relationships Trouble with post to user relationshipResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #30708
    Joseph WongJoseph Wong
    Participant

    Hi,

    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!

    #30710
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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.

    #30737
    Joseph WongJoseph Wong
    Participant

    Morning, 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

    #30763
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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 select user, 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,
    ] );
    #30771
    Joseph WongJoseph Wong
    Participant

    Hi 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!

    #30783
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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 function wp_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();
    #30785
    Joseph WongJoseph Wong
    Participant

    Hi 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.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.