Custom Fields
Re-Envisioned
Support MB Relationships Single metabox on Reciprocal Relationship

  • Creator
    Topic
  • #14412
    Resolved Content Pilot
    Participant

    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.

Viewing 10 replies - 1 through 10 (of 11 total)
  • Author
    Replies
  • #14416
    Anh Tran
    Keymaster

    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?

    #14417
    Content Pilot
    Participant

    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.

    #14425
    david.h
    Participant

    Hi Anh,

    I suggested a boolean for bi-directional (reciprocal) relationships in this post:

    https://metabox.io/support/topic/post-relationships/#post-7797

    Hope this helps,
    David

    #16102
    Jeremy Carlson
    Participant

    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

    #17570
    Content Pilot
    Participant

    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

    #17597
    Content Pilot
    Participant

    Hi Ahn, have you had a chance to think about this and read my comment on github?

    #17692
    Content Pilot
    Participant

    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.

    #17712
    Anh Tran
    Keymaster

    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.

    #17968
    Jeremy Carlson
    Participant

    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.

    #17992
    Anh Tran
    Keymaster

    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',
        ) );
Viewing 10 replies - 1 through 10 (of 11 total)
  • You must be logged in to reply to this topic.