• I am currently working on a functionality, where I have two custom post types. I want a bidirectional relationship between these two custom post types. But if that’s not enough, it should be possible for external users to create items of these posts and set up a relationship from a front end submission form without entering the wordpress admin dashboard.

    The first post type is a “property” and the second post type a “room”. The relationship is 1 to many. One property can have multiple rooms, but a room can just be connected to one property. The propert ist the “parent” and the room the “child”.

    On the single property page there should be a form to create a room connected to the property. Only visible for the author of the property. (I will not focus on user restrictions because that’s not the problem to set up properly)

    Because the project is just a test, I try to keep it simple and want to use free plugins and custom code as much as possible.

    The first suggestion I found was to use CPT UI in combination with the ACF plugin. The free version offers a relationship field, but not bidirectional. I have found some documentation about how to set up bidirectional relationships in ACF with custom code: https://www.advancedcustomfields.com/resources/bidirectional-relationships/
    In addition you can’t filter the related objects by the same author.

    However I continued searching and eventually found the PODs plugin. You can create CPTs, Taxonimies, etc. Great solution, because the birdirectional functionality is implemented, even in the free version. You can also filter the objects to connect which is missing in ACF. Also it has a gallery feature which is not included in the free version of ACF. Now I just need the front end submission form.

    Therefor I started with forminator because I wanted a user to be able to upload mulitple pictures when creating a cpt object from the frontend. Unfortunately there is no relationship feature. I have tried to get it working with a custom php code to filter the child post objects by the current user as the post author, so you can just select a property you have created. However the value won’t get submitted.

    Then I found the Frontend Admin plugin which allows to set up a relationship field, but not a gallery, because it’s connected with ACF in some way.

    Then I discovered the Jet Form Builder plugin which is a powerfull plugin in my opinion while it’s free as well. You can insert/update posts, users, send emails and much more stuff.

    I created a form to insert (create) rooms with all the fields I have added when setting up the CPT.
    There are options to create dynamic dropdown menus or select fields, but either you can just filter the objects of a CPT without the post author as the current user, or you need the “superduper plugin” JetEngine (not free).

    Thus I tried my first idea which is more elegant anyways. Simply put the form on the single post page of the property and then create the realtionship field based on the current post.
    There dozens of values to choose from when creating a hidden field such as Post_ID, Post_Title, Post_Meta, etc.
    I tried to connect the hidden field value to my custom field created in PODS.
    Creating a post works perfectly fine, but the Parent Post Object won’t get submitted and connected. I have tried Post, Post_ID, Post_Title, etc. Neither of them worked for me.
    Text fields will get the values and the current_post_id/title are querried correctly.

    However when I connect the hidden field to my ACF relationship field, it works. Now I am still missing the bidirectional relationship and the filtering.

    I don’t know, if there is a setting I am missing on in JetFormBuilder and/or PODs. Maybe you can’t create a post object with jet form builder from the front end and add the current post as a relationship in PODs.

    Maybe there are some plugins out there which will satisfy my problems.

    Thanks in Advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    PODs is closest to achieving the bidirectional relations you are after, correct? I take it there is no front end UI provided where relations can be defined? Find out how it maintains the relationships in the DB. You could then develop custom form handling code that saves relations in the exact same manner.

    Build you own front end form where relations can be defined and have the form submit to your form handling code. You can use another plugin to help you with a front end form, or build your own form from scratch.

    There are several ways a form can submit to reach your code. You could save via the REST API and a custom route/endpoint. You could save using Ajax techniques or submit through /wp-admin/admin-post.php. Or the form handling code could be part of a custom page template’s code. The template could even output the form as well. If your form properly structures its data, you could probably submit to where ever PODs forms submit to, saving you from needing custom form handler code.

    If you need help in determining how PODs saves post relations data, I suggest asking in their dedicated support forum for guidance. You might also ask if there’s any way to utilize their back end form on the front end.

    Thread Starter novakuso

    (@novakuso)

    Thank you for your quick answer.

    To be honest, I didn’t check the database on how PODs saves their relationship objects, because I figured that it would be faster to implement the bidirectional relationship functionality with Advanced Cusom Fields following their documentation.
    Indeed worked for me.

    I am still not fully satisfied, because PODs offers such a nice feature for free, without coding yourself. But for now it’s ok and I might take a closer look in the database in the future if needed.

    Regarding ACFs solution…

    I added a field group with a relationship field that covers both Post types and is displayed in both Post Type Edit Menus. Thus it’s possible to add mulitple properties to a room and even rooms to rooms from the backend. Not the functionality I wanted, but for a user from the frontend, it won’t be possible.
    Their documentation didn’t cover connecting multiple custom post types with each other, but this works for me as already said. I have tried setting it up with two different Field Groups and Relationship fields to seperate the Post Types and prevent the cases I mentioned earlier, where multiple properties can be assigned to a room. Haven’t found a solution yet.

    So far I am using Ajax for submitting the Jet Form Builder form.

    PODs unfortunatly won’t find a place in my project, not only because of my relationship issue, but the inbuild gallery feature doesn’ work, how it’s supposed to. I can add multiple images from the frontend and backend, but deleting a picture dosn’t delete it from the post. (Maybe a CronJob, Update method or Cache error?)

    I will search for help in their forum and will search for other plugins meanwhile.

    Moderator bcworkz

    (@bcworkz)

    If you use ACF to manage relations, you could determine how it saves defined relationships, the have a front end form that saves the data in the exact same way. I’ve not verified, but I’m guessing the relations are stored in postmeta as an array of post IDs. It’s not too difficult to save similar data from a front end form. The challenge might be in developing a nice UI which makes it easy for users to define relations.

    It can be frustrating when plugins don’t work exactly the way you want. You either need to revise what you want to fit what’s available, or customize what’s available to better fit your needs. Customizing plugins in some minor ways is usually feasible. But altering existing plugins in significant ways can be quite difficult. At some point it might be easier to develop a solution from scratch instead of getting existing code to conform to what you want. Where that point is depends on your coding skills and willingness to put in the time and effort.

    Thread Starter novakuso

    (@novakuso)

    Thank you for your help.

    I think I found a fitting solution. I deleted my settings from the post before, but sticked with Advanced Custom Fields.

    I am currently using the plugin ACF Post-2Post Plugin: https://wordpress.com/plugins/post-2-post-for-acf

    The setup worked for me.
    I now have to different field groups in which the posts of the other cpt are displayed.
    With the help of internet research and a little bit of chat gpt I implemented a filter. Now there is the option to only select a post as a relationship object that was created by the same post author.

    In the frontend users will automatically only be able to choose from posts they created.
    To prevent any errors during a submission, currently the post_id is submitted without the user even touching and seeing anything.

    If there is a better solution, where the effort and time pays of, I am willing to put it in and do some coding myself. I have a solid coding exprience and understanding and the will to learn new stuff. But in that regard I am not that familiar with php.

    Moderator bcworkz

    (@bcworkz)

    I don’t think I have adequate information to suggest a better solution. If what you have works for you, then it’s a good solution ??

    I too have a history of coding in various languages, but until recently PHP was completely alien to me. With backgrounds such as ours, I found PHP fairly easy to pick up. At least enough to customize various parts of a site. Initially I struggled with proper syntax, but it didn’t take too long to grasp the rules. Admittedly there are some structures and functions that are difficult to grasp, but in general it wasn’t long before I considered myself a PHP coder.

    I found the best way to learn a language is to have a real need for something custom coded. While formal tutorials and lessons are useful, I don’t seem to get much out of them. Real world projects is where I really learn. The trick is in finding a project that’s not too difficult. Ideally the project will be challenging, yet attainable.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Create Bidirectional Relationships from Front End’ is closed to new replies.