• Resolved samtronic

    (@samtronic)


    How to restrict to one-to-one relationship from users to terms? Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Anh Tran

    (@rilwis)

    Hi @samtronic,

    I’ve just pushed a change to Meta Box plugin that allows you to set one-to-one relationship. Please download the development version here:

    https://github.com/wpmetabox/meta-box/

    Then, in your code, set field’s parameter max_clone to 1, like this:

    add_action( 'mb_relationships_init', function () {
    	MB_Relationships_API::register(
    		array(
    			'id'   => 'users_to_terms',
    			'from' => array(
    				'object_type' => 'user',
    				'field'       => array(
    					'name'      => 'Manages',
    					'max_clone' => 1,
    				),
    			),
    			'to'   => array(
    				'object_type' => 'term',
    				'taxonomy'    => 'category',
    				'field'       => array(
    					'name'      => 'Managed By',
    					'max_clone' => 1,
    				),
    			),
    		)
    	);
    } );
    Thread Starter samtronic

    (@samtronic)

    Thank you so much!

    Thread Starter samtronic

    (@samtronic)

    Is there a way to lock the one-to-one relationship so a user can be linked only to an unassigned term and vice versa? Thanks!

    Plugin Author Anh Tran

    (@rilwis)

    To lock the relationship, you might need to set the “std” value for the field (the “field” array in the code above) and set the field read-only via custom attributes.

    Thread Starter samtronic

    (@samtronic)

    Thank you for your help ??

    What I meant to say is how to dynamically lock a user to a term. For example to lock the user “john” to the term “johnsmith” so the said term becomes unavailable to the rest of the users, and the said user becomes unavailable to the rest of the terms.

    It would be very helpful to have a “relationship_type” parameter with the options “one_to_one”, “one_to_many” and “many_to_many” like this:

    add_action( 'mb_relationships_init', function () {
      MB_Relationships_API::register(
        array(
          'id'   => 'users_to_terms',
          'relationship_type' => 'one_to_one'
          'from' => array(
            'object_type' => 'user',
            'field'       => array(
              'name'      => 'Manages',
            ),
          ),
          'to'   => array(
            'object_type' => 'term',
            'taxonomy'    => 'category',
            'field'       => array(
              'name'      => 'Managed By',
            ),
          ),
        )
      );
    } );

    The relationship_type would take take of the “max_clone” parameter accordingly.

    Please consider this suggestion. Thanks a lot!!!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘One-to-one relationship’ is closed to new replies.