Custom Fields
Re-Envisioned
Support MB Relationships User to User n:n relationship based on User role

This topic contains 1 reply, has 2 voices, and was last updated by  Anh Tran 3 months, 2 weeks ago.

  • Creator
    Topic
  • #11799
    Resolved
    aristo
    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.

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #11809

    Anh 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',
                ),
            ),
        ) );
    } );
Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.