Custom Fields
Re-Envisioned
Support MB Relationships How do I create a user to user relationship based on role?

  • Creator
    Topic
  • #31889
    Omni
    Participant

    Found a solution on the forum, however...

    
    add_action( 'mb_relationships_init', function () {
        MB_Relationships_API::register( array(
            'id'   => 'stud_to_teach',
            'from' => array(
                'object_type' => 'user',
                'query_args' => array( 'role__in' => 'student' ),
                'meta_box'    => array(
                    'title'       => 'Manages',
                    'field_title' => 'Select Users',
                ),
            ),
            'to' => array(
                'object_type' => 'user',
                'query_args' => array( 'role__in' => 'teacher' ),
                'meta_box'    => array(
                    'title'       => 'Manages users',
                    'field_title' => 'Select Users',
                ),
            ),
        ) );
    } );
    

    This shows the field on both user role's profile. Can I make it show vice versa?
    Eg. Teacher's profile can choose student, student can choose teacher

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #31897
    Long Nguyen
    Moderator

    Hi Omni,

    You can use the extension MB Include Exclude to show the relationship field on the specific role. For example:

    add_action( 'mb_relationships_init', function () {
        MB_Relationships_API::register( array(
            'id'   => 'stud_to_teach',
            'from' => array(
                'object_type' => 'user',
                'meta_box'    => array(
                    'title'       => 'Select Teacher',
                    'include' => array(
                        'user_role' => 'student'
                    )
                ),
                'field' => array(
                    'query_args' => array( 'role__in' => 'student' ),
                )
            ),
            'to' => array(
                'object_type' => 'user',
                'meta_box'    => array(
                    'title'       => 'Select Student',
                    'include' => array(
                        'user_role' => 'teacher'
                    )
                ),
                'field' => array(
                    'query_args' => array( 'role__in' => 'teacher' ),
                )
            ),
        ) );
    } );

    In the user profile of the student role, there is only a relationship field to select teacher and vice versa.
    Please read more on the documentation
    https://docs.metabox.io/extensions/meta-box-include-exclude/#settings
    https://docs.metabox.io/extensions/mb-relationships/#using-code

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.