Connecting Related Custom Post Types With Frontend Form

Support MB Frontend Submission Connecting Related Custom Post Types With Frontend FormResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #19246
    Tobias FueterTobias Fueter
    Participant

    We are running a dog shelter. I am building a website where our 'doctors' (users) can browse 'dogs' (custom post type 1) on the dogs archive page in the front end.

    With a form on the single page of the dog, the doctors should now be able to add a new 'medical report' (custom post type 2).

    This new 'medical report' entry should then be 'attached' to the dog. So a relationship between the dog and the medical report should be created when saving the form.

    This allows our doctors to look for a dog in the front-end, create a new medical report for that dog and attach it to the dog. I then can show all medical reports related to a dog on the dog's single page.

    I am aware of the 'MB relationship plugin'.

    But I don't know how to create a relation between the two custom post types 'dog' and 'medical report’ and then use a frontend form, so it automatically creates a relationship between the two entries.

    THX for your help!
    Tobias

    #19252
    Long NguyenLong Nguyen
    Moderator

    Hi Tobias,

    Thank you for contacting us. Hope you are well today.

    The extension MB Relationships supports to create the relation in the backend. In the frontend submission form, thank you for the idea, I'm going to discuss with the developer team and create a request feature to support relationship in the frontend.

    Have a nice day.

    #20893
    Long NguyenLong Nguyen
    Moderator

    Hi Tobias,

    Each relationship meta box has an ID: {$relationship_id}_relationships_from and {$relationship_id}_relationships_to. You can include these IDs in the form shortcode to show it on the front end.

    #32228
    Margaret BlauveltMargaret Blauvelt
    Participant

    Anything new with MB for this question?

    I have a similar situation. I have a custom post type 'events' (created by the Events Manager plugin) and another, 'instructor' (created via Meta Box). I used MB Relationships to generate code for a bidirectional (many-to-many) relationship. Works beautifully in the back end, with a drop-down box for Instructors appearing on Add and Edit Events pages and a column for instructors on the Events list and one for Events on the Instructors list (which I may remove from that Events list but it's nice to see it working in both directions).

    Now I am trying to figure out how to add one or more instructors to a given event being added or edited on the front end. If a user could add the instructors on the front end exactly as happens on the back end, that would be fine, though I already have coded a multiple-select list showing all possible instructors. Once the user picks the instructors that belong to the event, how do I save the instructor(s) to that event record? Ideally, the user would press Submit just once, which would create or update the Event record at the same time that one or more records are created to link that event to the chosen Instructors.

    Please point me in the right direction. I am quite new with WordPress and somewhat familiar with PHP and JavaScript (though I have used other programming languages much longer).

    Thanks!

    #32234
    Long NguyenLong Nguyen
    Moderator

    Hi Margaret,

    For example, you've created a relationship between events and instructor post types with this code

    add_action( 'mb_relationships_init', 'your_prefix_function_name' );
    function your_prefix_function_name() {
        MB_Relationships_API::register( [
            'id'   => 'events-to-instructor',
            'from' => 'events',
            'to'   => 'instructor',
        ] );
    }

    When you create a events post on the frontend via the frontend submission form, you can add the meta box ID events-to-instructor_relationships_to to the shortcode to show the relationship box to select relation instructor.

    [mb_frontend_form id='your-meta-box-id123,events-to-instructor_relationships_to' post_fields='title,content' post_type='events']

    Please read more on the documentation https://docs.metabox.io/extensions/mb-frontend-submission/

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