Support Forum
Support › MB Relationships › Single metabox on Reciprocal RelationshipResolved
Hi Ahn,
Until now, I have been using this plugin to relate an object in post type A with an object in post type B. This works great b/c the plugin creates a to and from metabox on each post type edit screen. I am now being ask if we can have a reciprocal relationship between two objects in the same post type. From the initial testing and reading the docs, both the to and from meta are being created. is there any way or do you have an idea of how to get it down to a single metabox when performing this type of relationship. I would be willing to submit a PR on github if we can talk through how this could be accomplished.
My initial thought was to add a flag in the metabox registration like reciprocal => true
and then that disables the default metabox output. Then you can insert the single metabox which is a combination of the two and from objects mashed together somehow. Does that sound like a good approach? What do you think? This is a must have for the platform I am building btw. Really appreciate your time.
Hi Clayton,
Thanks a lot for your valuable feedback! I didn't think about the 2 identical meta boxes appear on both sides (since it still works).
I like the idea of adding reciprocal => true
to the relationship. It's simple and easy to understand. My additional thought might be we should let users define only one meta box instead of 2. Would you mind helping me on Github?
For sure. I was expecting to be a big part of it. I greatly appreciate you guiding the way. Let's come up with a strategy and start a branch.
Hi Anh,
I suggested a boolean for bi-directional (reciprocal) relationships in this post:
https://support.metabox.io/topic/post-relationships/#post-7797
Hope this helps,
David
Hi there, I just wanted to check in — has anyone made progress on this concept? I'm working on a project where the relationship is very much reciprocal, and would love to use this.
In my case, the concept is plants which are "good companions" of each other. If tomatoes are good companions of basil, basil is automatically a good companion of a tomato.
I'm also considering implementing creating the reciprocal connection programmatically, but not quite sure how to go about that. New to MB.
Thanks for any input!
Jeremy
Hi Ahn, I made another comment on a merged PR for this topic. Your feedback is appreciated.
https://github.com/wpmetabox/mb-relationships/pull/22#issuecomment-566784736
Hi Ahn, have you had a chance to think about this and read my comment on github?
UPDATE.
A branch was created at GitHub.
Anh, I checked out the branch and did some initial testing in the admin, DB, and your frontend test. I noticed in the database that when you save the post, it places that post ID into the from
column and if you save another post, it flips and places itself into the from
column. The frontend WP_Query works both ways so I am a little lost. I see in the commits you adjusted the query.
Should we leave it as is or do the first post always need to be in the from
column and not switch around on every save_post action? That is how posts2posts works.
When you use reciprocal relationships, the "from" and "to" don't matter anymore. So you can use WP_Query on the front end for either side and it will work.
When saving into the database, the plugin will delete existing connections first, then re-add new connections. It doesn't care about the "from" or "to" side. That's why you see it flips.
Hi Anh,
Excited to see you've incorporated Reciprocal Relationships. I'm running into some problems with it, though — I'm getting duplicate relationships listed. Does this look right for registering the relationship?
MB_Relationships_API::register( array(
'id' => 'good_companions',
'reciprocal' => true,
'from' => array(
'object_type' => 'post',
'post_type' => 'plant',
'admin_column' => "after title",
'meta_box' => array(
'title' => esc_html__( 'Good Companions', 'gtt-text' ),
),
),
'to' => array(
'object_type' => 'post',
'post_type' => 'plant',
'meta_box' => array(
'hidden' => true,
),
),
) );
This was working for me before, and I'd been able to create the reciprocal relationship using the rwmb_after_save_field
hook. I've removed that code, but now each time I save a plant
post, the list of good_companions gets larger - at first glance, it seems to be adding a new instance of all related plants to the list each time.
For now, I'm rolling back to 1.10.10.
Hi, can you try removing all custom code, and change the registration code to something like this:
MB_Relationships_API::register( array(
'id' => 'good_companions',
'reciprocal' => true,
'from' => array(
'object_type' => 'post',
'post_type' => 'plant',
'admin_column' => "after title",
'meta_box' => array(
'title' => esc_html__( 'Good Companions', 'gtt-text' ),
),
),
'to' => 'plant',
) );
Thanks Anh,
I tried doing that but saw the same results. Looking at the wp_mb_relationships
table more closely, I see that all of my existing plants already have two rows for each relationship. So I suspected that was causing the duplication.
Trying a new plant, I did not see the same duplication (using your registration code above, AIO v.1.10.16) but I did see another issue, which I perhaps should have mentioned earlier -- I'm seeing "Bad Companions" show up which are duplicates of the Good Companions.
Yes, I have a second reciprocal relationship set up for plants <-> plants, as we also want to know what plants do not grow well together. The setup is similar.
I'm exploring this further, as I'd like to be able to upgrade simply...