[Resolved] Automatically set parent post ID in new child post Form using output from a View

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to automatically select the parent post in a new child post Form. I have a View that returns the correct parent post ID, so I would like to use that View in the Form shortcode but it does not seem to be working correctly.

Solution: It is not possible to set the parent post ID using an attribute in the cred_form shortcode, only in the parent post cred_field shortcode. You have the following options:

1. In the URL of the page containing the New House Form, add the correct parent post ID in a URL parameter. Use the urlparam attribute in the parent post cred_field shortcode to supply the name of the corresponding URL parameter. Example where the URL parameter is areaid:

[cred_field field='@book-chapter.parent' urlparam='areaid' class='form-control' output='bootstrap' select_text='--- not set ---']

2. Set the parent post ID in the value attribute on the parent post cred_field shortcode. Example where the parent post ID is 1234:

[cred_field field='@book-chapter.parent' value='1234' class='form-control' output='bootstrap' select_text='--- not set ---']

Relevant Documentation:
https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_form
https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_field

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 3 replies, has 2 voices.

Last updated by Puntorosso 1 month ago.

Assigned support staff: Christian Cox.

Author
Posts
#1638057

If I have a one to many relationship

CPT "Area" -> CPT "Houses"

And a view "area-of-agent" that retrieves the Area ID where the author is "John Doe".
Disable the wrapping DIV around the View is already marked, and the view is limited to 1 item, no formatting

Now Agent "John Doe" want to add a new house to his Area while is in a totally different page/post.

I have tried to use this shortcode

[cred_form form="add-house" post='[wpv-view name="area-of-agent"]']

But it doesn't work.

How do I set the parent post ID in this case?
Thanks

#1638641

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hello, it is not possible to set the parent post ID using an attribute in the cred_form shortcode. Check the documentation for the cred_form shortcode here:
https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_form
You'll see here the "post" attribute is used to provide a post ID when you are editing a post, not creating a new child post. You can set the parent post ID automatically in two ways:
1. In the URL of the page containing the New House Form, add the correct parent post ID in a URL parameter and use the urlparam attribute in the parent post cred_field shortcode to supply the name of the corresponding URL parameter, like this example where the URL parameter is areaid:

[cred_field field='@book-chapter.parent' urlparam='areaid' class='form-control' output='bootstrap' select_text='--- not set ---']

2. Set the parent post ID in the value attribute on the parent post cred_field shortcode, like this example where the parent post ID is 1234:

[cred_field field='@book-chapter.parent' value='1234' class='form-control' output='bootstrap' select_text='--- not set ---']

In your case it sounds like #2 is the best option, since it does not require you to modify the links pointing to all the pages containing this Form. Assuming your View returns the correct value without formatting, you may be able to place the View inside this cred_field shortcode's value attribute to set the proper parent post ID. If that is not working correctly, I'll be glad to take a closer look.

See the documentation for the cred_field shortcode here: https://toolset.com/documentation/user-guides/front-end-forms/cred-shortcodes/#cred_field

#1638701

Sorry I am a bit confused now.
I understand the cred_field shortcode set the parent-id of the form
Do I have to add it on the same page as the cred_form code?

Something like
[cred_field field='@area-house.parent' value='[wpv-view name="area-of-agent"]' class='form-control' output='bootstrap' select_text='--- not set ---']
[cred_form form="add-house"]

#1638783

Forget my last message. Now I have understood that it's meant to be used in the Advanced section of the form 🙂

Thanks you very much for your support and help.
Have a nice day.