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
- This topic has 13 replies, 2 voices, and was last updated 3 years, 2 months ago by
Long Nguyen.
-
AuthorPosts
-
March 2, 2022 at 6:48 PM #34232
Yasmine
ParticipantHello -
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
March 3, 2022 at 10:54 AM #34248Long Nguyen
ModeratorHi Yasmine,
What is the type of the field
universities
? Is it apost
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/
March 4, 2022 at 12:00 AM #34278Yasmine
ParticipantHi 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
March 4, 2022 at 12:08 AM #34279Yasmine
ParticipantThere 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?
March 5, 2022 at 9:46 AM #34321Long Nguyen
ModeratorHi 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.
March 6, 2022 at 6:19 PM #34342Yasmine
ParticipantThank 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?
March 7, 2022 at 5:16 PM #34360Long Nguyen
ModeratorHi,
- You can add more meta box (field group) IDs to the frontend submission form instead of using more frontend submission forms.
- 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.
[mb_frontend_form id="{$relationship_id_AB}_relationships_from,meta-box-ID1,meta-box-ID2" post_fields="title,content" post_type="B"]
March 7, 2022 at 5:27 PM #34361Yasmine
ParticipantOh ok - so I can add that to my user frontend form. Got you.
- 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?
March 8, 2022 at 12:53 PM #34388Long Nguyen
ModeratorHi,
The relationship works like the field
post
, maybe you are understanding the fieldpost
is the related post. There are some differences between relationship and fieldpost
but the main is
- the fieldpost
is a user meta that saves data in the tablewp_usermeta
- the relationship is a custom connection that saves data in the custom tablewp_relationships
please read more here
https://docs.metabox.io/fields/post/#data
https://docs.metabox.io/extensions/mb-relationships/#databaseYou can use the WP_Query to display the connected post with relationship, follow this documentation https://docs.metabox.io/extensions/mb-relationships/#users
March 9, 2022 at 5:53 AM #34412Yasmine
ParticipantO
March 9, 2022 at 6:02 AM #34413Yasmine
ParticipantYes, 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.
March 10, 2022 at 8:35 AM #34440Long Nguyen
ModeratorHi,
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.March 10, 2022 at 7:28 PM #34448Yasmine
ParticipantOk 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
March 11, 2022 at 12:23 PM #34469Long Nguyen
ModeratorHi,
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. -
AuthorPosts
- You must be logged in to reply to this topic.