Support Forum
Support › MB Relationships › How to Insert/Edit Relationship Field Manually? What's this Field ID?Resolved
Hello,
I have a CPT (Atendees) with several custom fields which is related to another CPT (Courses). I've managed to connect these fields in the backend, but now I need to populate the Atendees via front end form.
For the fronend I've used Fluent Forms which has MB support and I've succesfully managed to map and populate all the fields - all except the relationship.
I'd need to know:
The name of the relation is termin-pro-prihlasku, so I tried to map the field termin-pro-prihlasku_to (which was the ID of the respective admin column) and populate it with relevant Course ID. Unfortunately, it didn't work.
I've also tried MB Frontend Submission shortcode, but it didn't show the Relationship field at all - is MB Frontend Submission compatible with relationship?
Thank you for your reply.
Petr
Hi,
Please refer to this topic https://support.metabox.io/topic/mb-relationships-mb-frontend-submission/
to know how to include the relationship in the frontend submission form.
Hi Long,
I'm afraid, I cannot make it work, neither in the MB Frontend Submission form, nor in the Fluent Forms.
If my relationship ID is termin-pro-prihlasku and I want to use the From metabox, what should be the shortcode for the form?
I've tried this:
[mb_frontend_form id="prihlasky, termin-pro-prihlasku_relationships_from"]
and even this (in case I didn't comprehend the instructions):
[mb_frontend_form id="prihlasky, {$termin-pro-prihlasku}_relationships_from"]
But all I can see is the content of the "prihlasky" metabox.
And is it possible to access the relationship metafield directly, as I could the normal metafield for example the one with the ID course_term? I can map the normal fields in the Fluent forms, but not the relationship.
Can you please give me some further advice?
Thanks
Petr
Oh,
I've just made a discovery about the MB Frontend form - there must not be the spaces between the multiple IDs in the shortcode, only comas. (Despite the instruction you have made here https://support.metabox.io/topic/mb-relationships-mb-frontend-submission/#post-24285).
This shortcode works fine:
[mb_frontend_form id="prihlasky,termin-pro-prihlasku_relationships_to"]
(Yes, it turned out, I have to use TO not FROM - this part is still a bit confusing to me, it's really unintuitive to find out, which is which).
So, at least I can do it via MB Frontend. But I would really like to use Fluent Forms, because of it's other features. Is it possible to map the relationship there? Does the relationship field have its record in the wp_postmeta table? Or am I able to at least populate it via some helper PHP function?
Thanks
Petr
Hi,
MB Relationships data is stored in a custom table instead of the table wp_postmeta
, please read more on the documentation https://docs.metabox.io/extensions/mb-relationships/#database
so I don't think FluentForm can work with this data.
You can contact FluentForm support to ask for help with this case, they are the team who created the integration with Meta Box.
Hi Long,
OK, forget about the Fluent Forms.
How can I, on my own, edit the relationship field manually using PHP code?
Thanks
Petr
Hi,
You can use the setting field
to customize the relationship field by using the code. Please read more on the documentation https://docs.metabox.io/extensions/mb-relationships/#syntax
or using the builder https://docs.metabox.io/extensions/mb-relationships/#field-settings
Hi Long,
Well, to be honest, I did expect something more specific than a link to the superlong documentation on how to CREATE the relationship table.
Again, I wanted to EDIT the connection of already created relationship. I have created the relationship named "termin-pro-prihlasku" which interconnects 2 CTPs "courses" and "atendees".
In ACF it's quite straightforward, it's basically the same like editing all the other custom fields:
update_field('relationship_field', $value, $post_id);
After reading several pages of the documentation I've found the single line of code with very little explanation:
MB_Relationships_API::add( $from, $to, $id, $order_from = 1, $order_to = 1 );
It is based in the section here: https://docs.metabox.io/extensions/mb-relationships/#creating-connections-programmatically (in fact, this was the link, I was looking for).
Could you give me some example on how to uses it?
- Does it work only for creating the new relationships?
- What if I want to CHANGE some existing relationship?
- What are the two "$order" parameters for?
Thanks again
Petr
Hi,
Sorry for the misunderstanding.
Because Meta Box and MB extensions are very highly customizable so when you ask "edit the relationship field manually using PHP code", I think you want to customize the select field in the relationship box as you can read here https://docs.metabox.io/extensions/mb-relationships/#syntax (name, placeholder, query args ...)
So you want to create a "connection" automatically between two posts programmatically, is that right? You can
add
API in a callback, hook to some action like save_post
, rwmb_frontend_after_process
to use the passing ID.https://developer.wordpress.org/reference/hooks/save_post/
https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions
delete
, then add a new connection with API add
.
Hi Long,
thanks for your reply, it seems to be working now.
Just a little additional question: is it necessary to use the $order_from and $order_to?
I'm using this code and it seems to be working fine:
MB_Relationships_API::add( $postId, $termId, 'termin-pro-prihlasku');
Thanks
Petr
Hi,
If you want to add a connection between two posts and want to show it first on the frontend, you can use the order parameter. To understand it, please check the table wp_mb_relationships
screenshot https://monosnap.com/file/Ih5NVXLMoNxIlve6lZZXnD9SzJddOV
By default, no need to set up the order parameter.