Create Bidirectional Relationships from Front End
-
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!
- The topic ‘Create Bidirectional Relationships from Front End’ is closed to new replies.