How do I populate a relationship value based on the related post they select

Support MB Relationships How do I populate a relationship value based on the related post they select

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #34232
    YasmineYasmine
    Participant

    Hello -

    My user selects a related post from the frontend form (universities) - but I notice that this does not then save the relationship between the user and their associated university - am I missing an easy trick?

    If not, then how do I save the value of the related post into the relationship post field? I would want this to be updatable by the user too.

    Thank you, Yasmine

    #34248
    Long NguyenLong Nguyen
    Moderator

    Hi Yasmine,

    What is the type of the field universities? Is it a post field or a relationship box?

    If you want to display the relationship box in the frontend submission form, please refer to this topic https://support.metabox.io/topic/mb-relationships-mb-frontend-submission/

    #34278
    YasmineYasmine
    Participant

    Hi Long,

    Really after some advice - as you already helped me with a shortcode to display a related post, and would ideally keep it as is. But it was my understanding at the time that it would be possible to establish the relationship from their selection. Is there any way to just populate the relationship field depending on the related post they select?

    I saw something about dynamic population. Could that be a solution for this case or would you advise otherwise?

    University is a post. To clarify what I am trying to do: I would like the user to choose and be connected to a CPT (university). The post type university would then show the users connected to it. Eventually the university will be connected to another CPT called country. I would then like this 'country' to also be connected to the user. So that on another CPT where the user submits research, it will show their associated university and country. And clicking through to the country will show the universities of that country, then clicking through to each university, will show its associated academics. I have done nothing additional than create a relationship between user and university.

    The thread you have linked me to has a lot of information, but I still don't understand. Where do I add the [mb_frontend_form id="{$relationship_id_AB}_relationships_from, {$relationship_id_BC}_relationships_to" post_fields="title,content" post_type="B"] shortcode into my form? I would like it to be in the same form as my other form questions - or is this not possible? If it is, how do I add a metabox post relationship field with select 2 selection?

    Thank you

    #34279
    YasmineYasmine
    Participant

    There is also a loom video that you include on your last post. I can't access this, but videos really help me to cut through the jargon. Would you mind resharing it?

    Also, for the record. I have currently created a relationship from university to user (as there can be many users within a university, but a user is only associated to one university) - is this the correct direction?

    #34321
    Long NguyenLong Nguyen
    Moderator

    Hi Yasmine,

    Here is a demo record to include the relationship meta box in the frontend submission form https://monosnap.com/file/hqNFvpaWW0h5cbwV83tPmVnahzvJR2

    And this form is a separate form, it is not possible to add it into another form.

    #34342
    YasmineYasmine
    Participant

    Thank you for sharing that. It is clear how to add it to the frontend.

    But having it as a separate form makes it a bit clunky - with there being two submit buttons on same page, or having it split over two pages..

    Going back to my original question - is there a way to auto populate and save the value of the relationship depending on the related post that the user selects?

    #34360
    Long NguyenLong Nguyen
    Moderator

    Hi,

    1. You can add more meta box (field group) IDs to the frontend submission form instead of using more frontend submission forms.
    2. [mb_frontend_form id="{$relationship_id_AB}_relationships_from,meta-box-ID1,meta-box-ID2" post_fields="title,content" post_type="B"]

    3. I don't understand this case. Do you mean to create a relationship between a user and a post based on the post field? Or create a relationship between a user and the current viewing post? Some screenshots would be appreciated.
    #34361
    YasmineYasmine
    Participant

    Oh ok - so I can add that to my user frontend form. Got you.

    1. Its possible I still don't understand the difference between related posts and relationships. But it is my current understanding that I need the relationship field to be populated, so I can display related posts.

    So when a user selects the related post, I want the relationship to be established automatically. So if related_post = Europe; then relationship between user and region would then also fill as Europe. So yes, exactly, I would like to establish the relationship based on the post they select in related posts. Is this possible? I read something about WP_Query() in the documentation - would that do this?

    #34388
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The relationship works like the field post, maybe you are understanding the field post is the related post. There are some differences between relationship and field post but the main is
    - the field post is a user meta that saves data in the table wp_usermeta
    - the relationship is a custom connection that saves data in the custom table wp_relationships

    please read more here
    https://docs.metabox.io/fields/post/#data
    https://docs.metabox.io/extensions/mb-relationships/#database

    You can use the WP_Query to display the connected post with relationship, follow this documentation https://docs.metabox.io/extensions/mb-relationships/#users

    #34412
    YasmineYasmine
    Participant

    O

    #34413
    YasmineYasmine
    Participant

    Yes, I was indeed referring to field posts as related post. And just to clarify I was talking about the custom field option "post" that allows you to select a related post. Sorry that my terminology was confusing!

    So in summary - they save in different tables - but both form a relationship that can later be queried in the traditional 1:n way.

    So my next question is: Do I even need to worry about saving a relationship between a user and a CPT (in addition to the field post) - or is there no point as they are already connected by the field post? What would you advise for this scenario? Thank you.

    #34440
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The relationship supports the bidirectional connection feature, which means when you select a connected post when editing a user and save. You go to edit that post and the relation box also displays the user connected.

    But the field post only supports displaying connected posts from that user only. When you edit the post, you do not see anything like the relationship.

    You can also create a query to show related posts with the field post as well.

    #34448
    YasmineYasmine
    Participant

    Ok I get that. And so to answer my question - you do recommend that I create an autosave hook. So when a user selects a field post, a relationship is also established. Which means I can use the bidirectional relationship, and yet the user chooses on the front end

    #34469
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Since you can use the relationships on the frontend with the extension MB Frontend Submission, no need to use the field post to select the relation posts.

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