Problem: I have a Form that allows Users to select a parent post, and I would like to filter the list of parent post options. Then I would like to associate the two posts using custom code.
Solution: You can do this with a generic field, the cred_save_data API, and the toolset_connect_posts API. You must be sure to set the generic field to use the option "persist":1, like this:
[cred_generic_field field='custom-parent-field' type='select' class='' urlparam=''] { "required":0, "validate_format":0, "default":[], "persist":1, "options":[ ...your explicit options here... ] } [/cred_generic_field]
You can access a generic field value in the $_POST superglobal during the cred_save_data callback. You won't have access to the new post's ID in the cred_before_save_data callback. The new post hasn't been created yet, so no id yet. You'll need both post IDs to connect them in a relationship, so you will use cred_save_data instead. If your field has the name "custom-parent-field" then your callback would look like this:
add_action('cred_save_data', 'cred_custom_callback_fn',10,2); function cred_custom_callback_fn($post_id, $form_data) { $forms = array( 1234 ); // if a specific form if (in_array($form_data['id'], $forms)) { if (isset($_POST['custom-parent-field'])) { $parent_id = $_POST['custom-parent-field']; $parent = toolset_connect_posts( 'relationship_slug', $parent_id, $post_id ); // your other callback code continues here } } }
Replace custom-parent-field with the name of your generic field. Replace 1234 with the ID of the Form, or a comma-separated list of Form IDs if you want to apply this same logic to more than one Form. Replace relationship_slug with the actual slug of this relationship.
Relevant Documentation:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
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)
This topic contains 1 reply, has 2 voices.
Last updated by 6 years, 6 months ago.
Assisted by: Christian Cox.