Skip Navigation

[Resolved] Automatically assign a parent post and custom field value in a CRED form that creates child posts

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

Problem: I have a CRED form that creates child posts. I would like to have the parent post be automatically assigned as the current post where the CRED form is displayed. I would also like a custom field value to be automatically the current User's name.

Solution:
There are two ways you can predefine a CRED form field value. You can use the "value" attribute, or you can use the "urlparam" attribute. The value attribute allows you to pass a value in using another shortcode, and the urlparam attribute allows you to capture a URL parameter variable and use that as the value.

You can use the wpv-user shortcode and the value attribute to predefine the user's name:

[cred_field field="your-field-slug" post="your-post-type-slug" value="[wpv-user field='user_firstname'] [wpv-user field='user_lastname']"]

The urlparam value will pass in a URL parameter value to any field:

[cred_field field='_wpcf_belongs_parentslug_id' urlparam='wpvpostparent' select_text='--- not set ---' class='form-control' output='bootstrap']

Then you can link to your CRED form page using the wpvpostparent URL parameter to specify a parent post ID:
http://yoursite.com/cred-form-page?wpvpostparent=12345

If you want to use the current post ID as the value of the parent field, you can use the wpv-post-id shortcode and the $current_page operator:

[cred_field field='_wpcf_belongs_parentslug_id' value='[wpv-post-id id='$current_page']' select_text='--- not set ---' class='form-control' output='bootstrap']

Pro tip: if you want to hide the predefined parent select field you can wrap it in a hidden div:

<div style="display:none;">
[cred_field field='_wpcf_belongs_parentslug_id' value='[wpv-post-id id='$current_page']' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-id
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user

This support ticket is created 7 years, 1 month ago. 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#590900
2017-11-18_LI.jpg

Hi,

-- CRED tells me to not use post types within pages but rather content forms, however the CRED wizard only allow selection of an associated post type! --

anyway: I would like to have the parent post be automatically assigned as the current post on which the CRED form is in.

also I would like the posters name to be automatically the users name.

Is this possible? How? Or can I look forward to it?

KR,

Lars

#591043

Hi, there are two ways you can predefine a CRED form field value. You can use the "value" attribute, or you can use the "urlparam" attribute. The value attribute allows you to pass a value in using another shortcode, and the urlparam attribute allows you to capture a URL parameter variable and use that as the value.

You can use the wpv-user shortcode and the value attribute to predefine the user's name:

[cred_field field="your-field-slug" post="your-post-type-slug" value="[wpv-user field='user_firstname'] [wpv-user field='user_lastname']"]

The urlparam value will pass in a URL parameter value to any field:

[cred_field field='_wpcf_belongs_parentslug_id' urlparam='wpvpostparent' select_text='--- not set ---' class='form-control' output='bootstrap']

Then you can link to your CRED form page using the wpvpostparent URL parameter to specify a parent post ID:
hidden link

If you want to use the current post ID as the value of the parent field, you can use the wpv-post-id shortcode and the $current_page operator:

[cred_field field='_wpcf_belongs_parentslug_id' value='[wpv-post-id id='$current_page']' select_text='--- not set ---' class='form-control' output='bootstrap']

Pro tip: if you want to hide the predefined parent select field you can wrap it in a hidden div:

<div style="display:none;">
[cred_field field='_wpcf_belongs_parentslug_id' value='[wpv-post-id id='$current_page']' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>

We have documentation about these shortcodes available here:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-id
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user

#591364

This appears to be suitable. I will see what I can do with this at the next opportunity. Thank you!