Custom Fields
Re-Envisioned
Support MB Relationships Add more than one relationship at a time (MB_Relationships_API::add )

  • Creator
    Topic
  • #22432
    Resolved JC
    Participant

    Hello,

    First of all, great plugin!

    I am programatically creating relationships between users and posts but in the documentation it only appears possible to add one relationship at a time. I can acheive what I want to using a loop but it’s not good for performance. Is there a more appropriate way to acheive the following?

        foreach($array_of_post_ids as $post_id) {
    
            MB_Relationships_API::add(
                $from       = $user_id,
                $to         = $post_id,
                $id         = 'metabox_relationship_id',
                $order_from = 1,
                $order_to   = 1
            );
    
        }

    I would ideally like to pass in a single array of post ids but this doesn’t appear to work.

    It’s not a big problem because my arrays are relatively small in size but would like to know if there is a better practice here? Even a hacky workaround would be better than a loop I feel.

    Many thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #22433
    Long Nguyen
    Moderator

    Hi JC,

    The function add helps you to create relationships between 2 specific objects (a user and a post). If you want to create relationships for all users and posts, please follow the Basic Usage https://docs.metabox.io/extensions/mb-relationships/#creating-relationships.

    #22435
    JC
    Participant

    Thank you for your reply.

    I have done this (registered a relationship between users and posts).

    My use case is this: a user pays for access to a post and upon payment the user is “added” to the post which gives them access to it.

    But if the user needs to be “added” to more than one post (if they buy access to 3 posts, for instance), I have to wrap the Add function in a loop to add user to post 1, post 2, post 3 and so on separately.

    My question still remains, can I “add” a user to more than one post in one request. Or am I limited to the loop solution, one by one?

    I hope I made it a little clearer this time.

    Or perhaps there’s a better way of achieving this and I’m missing something?

    Thanks again 🙂

    #22437
    Long Nguyen
    Moderator

    Hi,

    The plugin only supports adding a relationship for one user and one post at one time. We have to use the loop to repeat this function for each post.

    I’m going to create a feature request for the developer team to cover this case. Thank you.

    #22440
    JC
    Participant

    Ok thank you for your help. No worries, I’ll keep doing it the way I’m doing it for now.

    All the best

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