Single metabox on Reciprocal Relationship
Support › MB Relationships › Single metabox on Reciprocal RelationshipResolved
- This topic has 11 replies, 4 voices, and was last updated 5 years, 2 months ago by
Jeremy Carlson.
-
AuthorPosts
-
May 3, 2019 at 11:35 PM #14412
Content Pilot
ParticipantHi 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.May 4, 2019 at 10:12 AM #14416Anh Tran
KeymasterHi 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?May 4, 2019 at 10:46 AM #14417Content Pilot
ParticipantFor 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.
May 4, 2019 at 6:10 PM #14425david.h
ParticipantHi 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,
DavidSeptember 13, 2019 at 5:59 AM #16102Jeremy Carlson
ParticipantHi 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!
JeremyDecember 19, 2019 at 12:17 AM #17570Content Pilot
ParticipantHi 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
December 20, 2019 at 4:44 AM #17597Content Pilot
ParticipantHi Ahn, have you had a chance to think about this and read my comment on github?
December 30, 2019 at 10:23 PM #17692Content Pilot
ParticipantUPDATE.
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 thefrom
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.January 4, 2020 at 10:08 AM #17712Anh Tran
KeymasterWhen 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.
January 23, 2020 at 6:05 AM #17968Jeremy Carlson
ParticipantHi 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 aplant
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.
January 24, 2020 at 9:22 AM #17992Anh Tran
KeymasterHi, 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', ) );
February 8, 2020 at 10:35 AM #18198Jeremy Carlson
ParticipantThanks 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...
-
AuthorPosts
- You must be logged in to reply to this topic.