User to User n:n relationship based on User role

Support MB Relationships User to User n:n relationship based on User roleResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #11799
    aristoaristo
    Participant

    Hello Anh,

    So I'm trying to create a relationship between users as mentioned on this MB relationship plugin "Supports creating reciprocal relationships (posts-posts, users-users, …)." based on their roles.

    let's say there is a "student" and "teacher". I want to create a reciprocal relationship between them, saying "student" can choose "teacher" and vice-versa. with a single field.

    For now, I tried this code.

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

    what it's doing is creating 2 fields. and allowing me to add more, but it's showing every type of users. am I using the key user_role wrong? what should be the correct key?

    also I'm struggling with the logic of

    if logged_in_user is student then show only teacher. And if logged_in_user is teacher then show only student.

    #11809
    Anh TranAnh Tran
    Keymaster

    Hi Aristo,

    You need to put the user role inside query_args, like this:

    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',
                ),
            ),
        ) );
    } );
    #31855
    OmniOmni
    Participant

    This doesn't seem to work.. is there an update?

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.