Reciprocal relationship, I don't get it...
Support › MB Relationships › Reciprocal relationship, I don't get it...Resolved
- This topic has 4 replies, 2 voices, and was last updated 3 years, 8 months ago by
Shemzone.
-
AuthorPosts
-
February 15, 2022 at 9:25 PM #33915
Shemzone
ParticipantHi
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.February 16, 2022 at 11:08 PM #33944Long Nguyen
ModeratorHi,
The setting
reciprocalhelps you to avoid showing two meta boxes (connect to itself) when you set the relation from an object type to itself. Like thisadd_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
reciprocalfor different object types (CPT) like your case, the connection meta box only shows on thefromobject type page.February 18, 2022 at 9:08 PM #33985Shemzone
ParticipantHi, 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 youFebruary 19, 2022 at 9:58 AM #33999Long Nguyen
ModeratorHi,
Yes, it is possible. You just need to remove the setting
reciprocalwhen registering the relationship. If on aproject1post, you selectservice1post related then on the postservice1, it will showproject1connected automatically without redoing manually.February 19, 2022 at 7:45 PM #34012Shemzone
ParticipantHi
I got it, it works.
Thank you for your help -
AuthorPosts
- You must be logged in to reply to this topic.