How to Insert/Edit Relationship Field Manually? What's this Field ID?

Support MB Relationships How to Insert/Edit Relationship Field Manually? What's this Field ID?Resolved

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #34242
    petr@blackeye.cz[email protected]
    Participant

    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:

    • What is the ID of the relationshop field?
    • What format should I use to populate it?
    • Is it even possible?

    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

    #34260
    Long NguyenLong Nguyen
    Moderator

    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.

    #34265
    petr@blackeye.cz[email protected]
    Participant

    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

    #34266
    petr@blackeye.cz[email protected]
    Participant

    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

    #34289
    Long NguyenLong Nguyen
    Moderator

    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.

    #34304
    petr@blackeye.cz[email protected]
    Participant

    Hi Long,

    OK, forget about the Fluent Forms.
    How can I, on my own, edit the relationship field manually using PHP code?

    Thanks
    Petr

    #34324
    Long NguyenLong Nguyen
    Moderator

    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

    #34378
    petr@blackeye.cz[email protected]
    Participant

    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

    #34405
    Long NguyenLong Nguyen
    Moderator

    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

    1. Follow this topic https://support.metabox.io/topic/prepopulate-relationship-field-with-current-post-id/
      to use add API in a callback, hook to some action like save_post, rwmb_frontend_after_process to use the passing ID.
    2. https://developer.wordpress.org/reference/hooks/save_post/
      https://docs.metabox.io/extensions/mb-frontend-submission/#form-actions

    3. Delete a connection first with API delete, then add a new connection with API add.
    4. When you add more connected posts manually, you can re-order posts. This is the order parameter. Here is an example https://monosnap.com/file/OAPqvSNmOIC1T42MuLSYUKbo0nGAEf
    #34601
    petr@blackeye.cz[email protected]
    Participant

    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

    #34613
    Long NguyenLong Nguyen
    Moderator

    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.

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