Skip Navigation

[Resolved] Relationship form

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 8 replies, has 3 voices.

Last updated by Waqar 1 year, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2634673

This website has 3 custom post types: ships, ports and reservations. On the single post page of the port, a ship owner (author) should be able to make a reservation for that port.

The reservations should be shown on both the ships single post page and the port single post page.

How can I achieve this?

#2634731

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

My instinct would be to make a many-to-many relationship between ship and port posts (reservation posts would be an intermediate post type of the connection between ships and ports).

You would need to use relationship forms to connect the ship and port posts for each reservation.

But if you have an existing reservation post type, then it seems like you would connect ship and port posts to a reservation post via one-to-one relationships (each reservation connects a specific ship to a specific port).

From the port single post page you would have a form to publish a reservation post that included a "parent" selector for the port and another for the ship.

The port would be pre-selected (based on the current port post). If the current user is logged in and is the owner of the ship (and is the author of the ship post and only one ship post), then it should be possible to pre-select the ship, too.

Does that sound like the kind of solution you had in mind?

With either option—using a many-to-many relationship or two one-to-one relationships—you would use a View with a relationship filter to query the connected reservation posts, from which you can display details of the reservation.

#2634755

Hi Nigel,

I've set your first solution up now, that seems to work. However, I I would like to pre-select the ship as well as you describe, and I'm getting a critical error when I want to edit a reservation in the backend.

#2634965

Hi,

I'll recommend turning on WordPress debugging and then checking the server's error logs, to see details about the critical error.
( ref: https://wordpress.org/documentation/article/debugging-in-wordpress/ )

In case there are no detailed errors or warnings, you can share temporary admin login details with us, along with the exact steps to reach that critical error screen.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2635953

Thank you for sharing the access details.

I'm going to split my reply into two main parts:

Part 1:

In the admin area, I checked the intermediary post type 'Reserveringen' and found this one post under it:
"Reserveringen: 7621 – 1899"

It shows a fatal error when trying to edit because the data stored in the date field(s) of this specific post is not correct.

I created a new connection between the 'Groenbaard 1936' Schip and the 'Meppel' Ligplaats and a new Reserveringen "Reserveringen: 8062 – 7487" was created.

If you'll try to edit it in the admin area, you won't see the same fatal error.

Also, I see that the relationship form 'Reserveringsformulier' is added on the single 'Ligplaats' post pages. The current 'Ligplaatsen' is automatically selected which makes sense and the user can use the 'Schepen' field to select the Schip post to make the reservation. If this 'Schepen' field is also automatically selected, the user will have no choice to select which Schip to choose from.

Part 2:

Thinking the whole requirement through, I believe the current setup won't work effectively and it would be better to adopt Nigel's second approach recommendation.

In the current setup, a ship and a port post, are connected directly through a many-to-many relationship 'Reserveringen'. But if in the future another user wants to make another reservation with the same ship and same port it won't be possible, because 1 connection between them already exists.

The way many-to-many relationships work, a connection between two specific posts can't be created again and a connection is not automatically disconnected after a certain time either.

A better setup would be:

3 Post types:

a). Ships
b). Ports
c). Reservations

2 one-to-one relationships:

a). Reservations <-> Ships
a). Reservations <-> Ports

This way each post in the 'Reservations' post type will be a truly unique and independent entity. And information about a particular reservation like start and end dates will be saved with this 'Reservations' post type.

I hope this helps and please let me know if you need further assistance.

#2636011

Hi,

Okay, I've created the 3 post type setup now. But how do I create the form to make a reservation now?

With the relationship form, I can only create 1 relationship, but I need both relationships in 1 form because a reservation should contain both the ship and the port.

I can also use a post form, but with that, I can't seem to pre-select the port or ship?

#2636659

> I can also use a post form, but with that, I can't seem to pre-select the port or ship?

- You can create two identical forms to add a 'Reserveringen' post, for example:

a). Form to add Reserveringen in port
b). Form to add Reserveringen in ship

Both will have the same fields, including the fields to select a port and a ship.

The only difference would be that:

a). In the form 'Form to add Reserveringen in port' (which will be used on the single port page), you'll add the current post's ID shortcode [wpv-post-id] ( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-id ) in the default value for the port selection field

b). And, in the form 'Form to add Reserveringen in ship' (which will be used on the single ship page), you'll add the current post's ID shortcode [wpv-post-id] in the default value for the ship selection field

As a result, the current port and the current ship will be selected in the respective fields, in each form, depending on where they are used.

#2639765

I only need a reservation form on the port page, but it should register which ship it's about. I have a form now which works on the front-end, but it only saves either the ship OR the port in the back-end. It never saves both.

Seems it will only save the ship on the first reservation ever for that particular ship, after that, it only saves the port (even with a different port). I can't select a ship I've made a reservation for in the backend either.

#2639999

Thanks for writing back.

Thinking this through, it makes more sense that the relationships are changed to a 'one-to-many' type.

Can you please delete the existing relationships and add them again as:

2 one-to-many relationships:

a). Ships -> Reservations
b). Ports -> Reservations

This way, each reservation post will be able to connect to only one ship and one port. However, those ships and ports will be available to be connected to other reservation posts too.

#2640053

Yes, that seems to work fine, thanks!