• I have an application where I need a one-to-many relationship maintained in a custom post type upon form submit.
    Custom Post types:
    1. Person
    2. Plants

    One person can have multiple plants but one plant can only be assigned to one person.

    I want a form to submit and respect this relationship. When the form is submitted I would like the following to happen:

    1. Check if the person exists in the ‘PERSON’ custom post type. If not, create the person and get the person_ID.
    2. Create the plant in the ‘PLANT’ custom post type.
    3. Update the relationship with the new person to plant relation.

    I know how to code this. What can the plugin do and what/where do I put the code for the rest? Which filters do I use?

    Thanks!
    Caroline

    Appreciate the help.

Viewing 1 replies (of 1 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    Hi Caroline, happy new year!

    One person can have multiple plants but one plant can only be assigned to one person.

    so this is possible, but not easy to achive without some coding.

    Here is what I suggest, use the UI mapper to map your form’s plant fields to create a new plant post. Hook the action fired at the end of the mapping process to get the new plant post ID, analyse the submission to determine if you need to create a new person post (programmatically), or else retrieve the existing person post ID, then programmatically link the newly created plant post to your person post using a meta-field.

    1. Check if the person exists in the ‘PERSON’ custom post type. If not, create the person and get the person_ID.

    if an existing person post can be determined prior to the form loading, (eg with the logged in user, or the page they loaded to reach the form) then you could add the person post ID as a hidden field to the form with the cf7 hook ‘wpcf7_form_hidden_fields’ fired at form loading time.

Viewing 1 replies (of 1 total)
  • The topic ‘1-to-many relationships for custom post types’ is closed to new replies.