Reciprocal relationship, I don't get it...

Support MB Relationships Reciprocal relationship, I don't get it...Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #33915
    ShemzoneShemzone
    Participant

    Hi
    I have created a relationship (projects-services) and ticked Reciprocal relationship checkbox.
    I call the relationship between projects and services from my projects page as the following:

    $services = MB_Relationships_API::get_connected( [
        'id'   => 'projects-services',
        'from' => get_the_ID(),
        'order'=> 'DESC',
        'order_from' => get_the_title(),
    ] );
    
    foreach ( $services as $service ) {
       echo $service->post_title;
    }

    Until so far, no problem but what I have to do if I want to display all projects related to a service from a service page?
    I don't get what the Reciprocal relationship checkbox is for?
    Or do I have to create another relationship between Services and projects and associate manually all the projects to a service? Double work then...
    Thank you to enlight me.

    #33944
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The setting reciprocal helps you to avoid showing two meta boxes (connect to itself) when you set the relation from an object type to itself. Like this

    add_action( 'mb_relationships_init', 'your_prefix_function_name' );
    function your_prefix_function_name() {
        MB_Relationships_API::register( [
            'id'   => 'post-to-post',
            'from' => 'post',
            'to'   => 'post',
        ] );
    }

    Screenshot https://monosnap.com/file/QwbCPB9qCcHF8ToqkNHDqjZbbzUWwU

    If you set reciprocal for different object types (CPT) like your case, the connection meta box only shows on the from object type page.

    #33985
    ShemzoneShemzone
    Participant

    Hi, thank you for your reply. So, if I understand correctly, reciprocal cannot be used for different custom types, right?
    So, in my case, I have a relationship between projects and services. A project can have several services. I can list services related to a project. But if I want to list all projects related to a service (a service can be related to several projetcs), how can I avoid to create an new relationship between services and projects and redo the work of select projects related to a service?
    Is it doable?
    I hope I am enough understandable.
    Thank you

    #33999
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Yes, it is possible. You just need to remove the setting reciprocal when registering the relationship. If on a project1 post, you select service1 post related then on the post service1, it will show project1 connected automatically without redoing manually.

    #34012
    ShemzoneShemzone
    Participant

    Hi
    I got it, it works.
    Thank you for your help

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