Skip Navigation

[Résolu] Create multiple posts with a single cred form

This support ticket is created Il y a 8 années et 5 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Marqué : 

This topic contains 3 réponses, has 2 voix.

Last updated by Beda Il y a 8 années et 5 mois.

Assisted by: Beda.

Auteur
Publications
#346355

I have a Parent Post Type Fulfillment with a child post type Restaurants-to-Orders.

I currently have a content template for the Fulfillment post type that displays a cred form to add a child Restaurant-to-Orders post to the fulfillment:

[cred_child_link_form form="2090" parent_id="-1" target="_self"]

Each fulfillment needs 3-4 child posts added which requires quite a few page reloads as the child posts can only be added one at a time.

What I'd like to know is if there is anyway with CRED to add multiple child posts to the fulfillment with a single form.

What I'd envisage is the entire CRED form would display as normal and an "Add additional Child" button would display at the bottom similar to the functionality of types fields that have the "Allow multiple-instances of this field" option checked.

When submitted the CRED form iterates over these and creates the multiple child posts.

If this isn't possible natively but it would be possible to do using the CRED api I'd love some direction as to how to start building this including which hooks I should be using. Any code snippet examples would be greatly appreciated.

#346499

Thank you for contacting us here in the Support Forum and for providing the Debug Informations

Each CRED form creates a single post with it's specific values stored.

If you want to submit multiple posts, you need a value for each of those CRED forms, let's say just minimal, the Post title

This is why you can not create multiple posts with one Form out of the box.

The most difficult is to have a feature that allows you to choose dynamically how many Child posts you are going to create.

If you have a limited number it would be very easy, as example with a Bootstrap Collapse Button on the "Create child CRED form" holding page, that holds all Child Forms you want the user to be able to create.

The user can submit the first form and you redirect him to the same page and so on

It could even be without the Bootstrap Collapse, by just redirecting after each submit to the correct "Create Child" page

It requires a page reload though, and the Posts aren't created all in one bunch.

With repeating Fields you could theoretically allow the user to add as many items of a certain field as he wants and then get this information to create Posts with the API, as example you have a Repeating Single Line Field and use the data to create (wp_insert_post) with each of the values.

the problem here is, the repeating field would be repeatable for each Field, not for each "post".
That means, your user chooses as example 2 "Titles", and then he goes on and chooses 2 Images, etc.

But it would not be a "good user experience", as the suer must think of both (or even more) posts simultaneously and not one after the other.
Also, some Custom Fields can't be repeated, such as Post Body or WYSIWYG.

I do not see any good and User Friendly way to add dynamically a certain amount of Posts with one form on the same "form"/page.

If your Posts can be the almost exact same though, I see a way of programmatically add (wp_insert_post) Posts with a red_save_data function.

You would as example set a Custom Field where you determine how many other posts the user wants to create.
You get this value and wp_insert_post for each of the values you already have, adding a "dynamic" extension to each, just like WordPress does when you create many posts with same title/slug
(post-2, post-3) or similar.

You could get the data from the "real", first form and enrich with eventually even values set in additional Fields the suer must set, if he wants more then one post.

I am eager to help, let us talk how "different" your content should be first, if that's OK for you?

Thank you for your patience.

#346571

Alright thanks for that overview explanation.

So currently our fulfillments require either 3 or 4 child "Restaurant-To-Order" posts depending on the product ordered. I currently load the CRED create child post form (full wp page with the page template modified to only display the form) via Ajax onto the fulfillment layout and the form redirects back to the fulfillment page upon completion so only 1 page load is required per child post. But even this adds up when our operators have to process 10 or 20 orders at once (up to 80 child posts).

Each child post created needs 3 fields submitted: two drop down select fields and a number field. I currently display these in a row (See annotated image below)

hidden link

So ideally I think I'd need a dropdown box to select the number of child posts to create (I could probably get smart and use jQuery to work out how many are required from the product name but manual selection would be fine for now).

That number would copy and create additional sets of form fields rows for the extra child posts. Then on submission we somehow need to process those additional rows.

Possible Alternative: Is there enough information on the fulfillment page itself to create a child restaurant-to-order post? As in could I build my own form outside of cred and pass it the fulfillment page id and run a custom function using php on submission: foreach "row" of form fields run wp_insert_post and add_post_meta to create the post and assign the relevant custom field data?

#346821

Thanks for the Details

As mentioned you could trigger this in a CRED save_data action, the thing is, you need somehow to populate your "unique" CRED form with the Data.

CRED provides to main accesses:

$post (what your currently create / edit)
$form_data (things like container_id etc)

This last is information on the "container" of CRED.

This things work only in CRED hooks.

What I also think could be working for you is a CRED conditional wrapped around additional Fields in your "main" CRED form

If your user chooses as example a select value "add 4 posts" you would display a part of the CRED form with 4 rows of fields for 4 posts.

But keep in mind, you can not just "replicate" this fields
They need unique values as otherwise they will just apply all to the same KEY in post_meta

So a suggestion could be to even create a Post Type, also child of, just as you current one, and with this Post Type CRED Add form you actually "gather" the data.

According this you then use CRED save_data and trigger a wp_insert_post in it, using the data of the fields provided in the Form.

Each field needs a unique value.

This is no two avoid, as otherwise WordPress won't know how to differentiate things.

Please let me know if you need further help with the CRED function

Thank you for your patience.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.