Require a relationship

Support MB Relationships Require a relationship

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #46619
    FizzPop MediaFizzPop Media
    Participant

    I have two post types in Metabox:
    1) Properties
    2) Communities

    I have a relationship between the two post types so that each property can be related to 1 Community.

    Currently, I don't see a way to require a relationship entry for a property. (I do see a way to set the max number of connections). Is there a way to require a relationship?

    If not, how else could I solve for this?

    #46631
    PeterPeter
    Moderator

    Hello,

    You can use the code to make the field in the relationship box mandatory. Here is an example:

    function your_prefix_function_name() {
        MB_Relationships_API::register( [
            'id'          => 'post-to-product',
            'from'        => [
                'object_type'  => 'post',
                'post_type'    => 'post',
                'field'        => [
                    'required' => true //here
                ],
            ],
            'to'          => [
                'object_type'  => 'post',
                'post_type'    => 'product',
                'field'        => [
                    'required' => true //here
                ],
            ],
        ] );
    }

    Following the documentation https://docs.metabox.io/extensions/mb-relationships/#using-code

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