Connecting Related Custom Post Types With Frontend Form
Support › MB Frontend Submission › Connecting Related Custom Post Types With Frontend FormResolved
- This topic has 4 replies, 3 voices, and was last updated 3 years, 4 months ago by
Long Nguyen.
-
AuthorPosts
-
April 23, 2020 at 9:30 PM #19246
Tobias Fueter
ParticipantWe 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!
TobiasApril 24, 2020 at 9:18 AM #19252Long Nguyen
ModeratorHi 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.
July 22, 2020 at 9:50 AM #20893Long Nguyen
ModeratorHi 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.November 29, 2021 at 11:57 AM #32228Margaret Blauvelt
ParticipantAnything 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!
November 29, 2021 at 1:21 PM #32234Long Nguyen
ModeratorHi Margaret,
For example, you've created a relationship between
events
andinstructor
post types with this codeadd_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 IDevents-to-instructor_relationships_to
to the shortcode to show the relationship box to select relationinstructor
.[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/
-
AuthorPosts
- You must be logged in to reply to this topic.