Skip Navigation

[Resuelto] Create multiple posts with a single cred form

This support ticket is created hace 5 años, 4 meses. There's a good chance that you are reading advice that it now obsolete.

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.

This topic contains 6 respuestas, has 3 mensajes.

Last updated by romanB-3 hace 5 años, 4 meses.

Assigned support staff: Nigel.

Autor
Mensajes
#469210

I had a post about this last year with Beda discussing the issue:

https://toolset.com/forums/topic/create-multiple-posts-with-a-single-cred-form/

After that discussion we used a stopgap measure of creating posts one by one with a CRED form but now that the site has grown and we're processing more orders that's not a feasible solution anymore and we need a way to create all the post relationship posts at once.

Beda suggested creating another CPT for this form and then intercepting the data with the save_data hook and creating the posts through there. I've started coding this solution up but have run into a problem.

For my basic fields, I can simply create 4 instances of these fields with different names and get all 4 instances of data through the save_data hook. What I can't work out how to do though is the same thing for a select field where the values are coming from my Restaurants CPT. If I create multiple instances of this field only one instances value is being passed into the save_data hook as they all have the same ID.

Can you suggest a way to create multiple instances of a select box of the restaurant CPTs in which I can access the 4 different values in the save_data hook?

#469321

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

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

Hi Grant

I have read through the previous post and this one and pretty much follow what you are aiming to do, i.e. to have a single form where you can create multiple child posts in one hit rather than create them one-by-one.

Can you just clarify for me what the fields are on your child posts?

The parent CPT is "fulfilment", the child CPT is "restaurant".

Your child posts need the fulfilment parent post id, plus what other fields. The select fields, where do the options come from?

#469470

Hi Nigel,

So I'm creating a one-to-many post relationship 'assigning' 3-4 restaurant CPT posts to a Fulfillment CPT post using a Restaurant-to-Fulfillment CPT.

So i'm looking to create 4 of these "in between" CPTs at once. These which will each have the same parent Fulfillment but have a different parent Restaurant.

I need 4 separate select dropdown fields to select the 4 different Restaurants for these posts which is where I'm stuck.

#469934

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

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

Hi Grant

So, the key here is that you need the restaurant ids to be available to the code you hook onto cred_save_data, yes?

If you were not worrying about creating multiple intermediate posts in one go and you were doing it one at a time, you would simply add the parent field selector for the fulfilment post plus the same for the restaurant post, yes?

And you can't add multiple instances of these to a single form for ex-post processing.

I don't know how many Restaurants we are talking about such that this would get out of control if you were to display the restaurants in a multi-select input field or as checkboxes so that the user could select as many or as few as required and then you loop through the selected options in your code and make as many intermediate posts as required.

Does that sound like a viable option? If so I can give you more details about how to display your restaurant posts as a form input.

#470122

Hi Nigel,

I had already specified in my last response that using a manual select input field wouldn't be a good solution. The client adds new restaurants by themselves and shouldn't need to add them in multiple places.

I had a brainwave this morning and worked out my own solution. I created a view for Restaurants CPT that set up a select field and looped through the Restaurant Post Type essentially recreating the output that my existing cred field was producing. I then added an incremental number to the select field name using a shortcode parameter. What that meant is that in my cred form I could use mutliple instances of the view with a different number to create unique copies of the field, ie:

[wpv-view name="restaurant-select-box" number="1"]
[wpv-view name="restaurant-select-box" number="2"]
[wpv-view name="restaurant-select-box" number="3"]
[wpv-view name="restaurant-select-box" number="4"]

This has allowed me to get the data I need in the cred_save_data hook and produce the required posts.

#470157

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

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

Hi Grant

I think you must have misunderstood my answer, I was suggesting something similar—using a view of restaurants to populate the form input, but with either checkboxes or multi-select so that the client could add a variable number of restaurants to the fulfilment order, but anyway, I'm glad you got this to work.

#552201

Hello,
I have a similar issue here and wonder if it would be possible to have a single form where I can create multiple child posts in one hit rather than create them one-by-one, thanks to Nigel's solution ?
Thank you.