Custom Fields
Support MB Relationships Custom Field that works with MB_Relationships API

  • Creator
  • #20580
    Resolved badabingbreda

    I have created (almost ready) a new Custom Field that can keep track of multiple relationships to one and same post id.

    As per another topic's requirement it would work like this:

    A class attendance (CPT) has one or student (CPT) that are either present, absent or late.
    There are three relationships, one for class_present_to_student, class_absent_to_student and class_late_to_student so that I can keep track of the classes the student attended, was supposed to be there but was absent and the ones that it was late.

    The way the regular relationships work is a bit awkward unfortunately. You get three meta boxes, and considering that you are on the class CPT, you need to select the student(s) for each state. If you mess up, you can have the same student in both present and absent, which obviously is not wanted.

    So, what I wanted to do worked up until the last part, an unfortunate miscalculation;

    At present it outputs a field as follows:

    [select2 student] [add student]
    Albert O Present O Absent O Late [delete]
    Bernhard O Present O Absent O Late [delete]
    Caroline O Present O Absent O Late [delete]

    [select student] is a select2-input field that queries the corresponding post_type so new students can be added to the end of the list using the [add student] button. Of course the [delete] button is used to remove the student in case it's needed.

    The field itself works as expected. It registers the settings and are used to render the field when reloaded. But here's where I made a misjudgement:
    I keep track of the changes in the field, the removed student ids, the ones that need an update (remove from state they're no longer in, add to new state). I meant to use those to use the API on the new Fields Class's save() callback, to MB_Relationships_API::add() and MB_Relationships_API::delete().

    The problem is , I think, that because the API also writes the Meta Box values generated by the Relationships API when it saves the post. I can't seem to be able to write my values using the save callback.

    Any ideas on how to temporarily disable the saving values in the meta boxes, so I can forcefully write the values to it using the API?

    I can share the code in a repo if you need to see what I'm trying to do.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Author
  • #20586
    Long Nguyen


    The extension MB Relationship does not support to track the change, I think you can use the field post and the feature Revision of WordPress to track the change of fields. See my screen record

    Please follow this documentation to enable the Revision for the custom field


    Thanks for that video, but it illustrates exactly what is not supposed to happen. You were able to set John as both absent and present.


    I continued on my quest to make it work. I managed to find a way to get most things to work as I wanted it, but I'm still hitting a bit of a snag.

    Please review my video first to see that it works.

    The MB_Relationship_API::register() command is there, but in order to make it stop registering it whenever I'm on admin and in the post_type 'attendance'. That way the metaboxes don't show up and don't save, thereby not interfering with my coded API::add() and API::delete()

    I do that by checking for is_admin() and get_post_type( $_GET['post'] ) == 'attendance' in the mb_relationships_init action-hook. If both true, the script bails early, thereby not registering the relationships.

    However, it currently only works on a UPDATE-action on the post. On the initial CREATE of a new attendance-post it doesn't seem to work. My guess is that the relationship IS registered (doesn't bail early) and overwrites my coded relationships.

    Any ideas to make this final piece of the puzzel? I have all the goodies from MB Relationships API, but need a way to block the relationships from overwriting my code when editing a post.

    Anh Tran


    I think there's a way to prevent the MB Relationships from saving to the database using the save_field parameter for field settings (I haven't tested it yet). This param tells Meta Box to not save field value to the DB. So, you can try it by specify it explicitly in the field param of the relationship.

    add_action( 'mb_relationships_init', function () {
        MB_Relationships_API::register( [
            'id'   => 'class_present_to_student',
            'from' => [
                'post_type' => 'class',
                'field' => [
                    'save_field' => false,
            'to'   => [
                'post_type' => 'student',
                'field' => [
                    'save_field' => false,
        ] );
    } );

    LOL, if only you had chipped in earlier... It does seem to work! Thanks for that!

    Great, now I can remove all my workarounds that do the important stuff that doesn't work when you don't register the metabox, namely adding, deleting and getting the connections... 🙂

    Hopefully my last question:
    Now that I have no use for the meta boxes but CAN use the relationships, is there a way to hide the meta boxes using a parameter while registering the relationship by any chance? I tried using the show/hide extension, targeting a input_value but to no avail. Currently writing CSS that echos with the field that hides them, but maybe there's already something for that.


    Okay, so thanks Anh for helping me out. I made this into a plugin available for download.

    Needs Meta Box and MB Relationships extension obviously; The plugin/field is NOT a replacement of the Relationships Field, but can be used to control multiple relationships like the one described in the repo's README.


    Okay, so maybe one or two more things:

    You've been very helpful with other extensions already so maybe you are willing to a add two action hooks, one for MBR_Relationship::add() and one for MBR_Relationship::delete() ?

    I see a situation that one might want to update a custom field to keep track of the total number of relations for a relationship id. Because changes can me made from both ends an action might be a better fit to do that once, so that it can easily be displayed without the constant overhead of counting the rows in wp_mb_relationships table at runtime.

    Another use for it would be to clear the cache on the post for which the relation has just been added or deleted. I don't think that's currently possible, since it doesn't keep track of changes.

    Anh Tran

    Hi Didou,

    To hide meta boxes, I think CSS probably is the fastest way. You can also hook to rwmb_meta_boxes or WP's add_meta_boxes hook to remove them but I think it requires more code.

    Regarding the action hooks, I can add them for you. But do you want to make a contribution on Github? So I can add a credit for you as a thanks!

    Anh Tran

    Just added 2 hooks for add and delete action:

    do_action( 'mb_relationships_add', $from, $to, $type, $order_from, $order_to );
    do_action( 'mb_relationships_delete', $from, $to, $type );
Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.