Problem: I have a Form that creates new child posts and allows the User to select the parent post. The parent post is set correctly based on the current page, but when the child post is created the relationship is not saved and the child post has no parent post.
Solution: There is currently a bug affecting post relationships in a new child Form when a Layout is used to design the page. Here's a snippet that you can add to your child theme's functions.php file temporarily to overcome the issue:
add_action('cred_save_data', 'toolset_fix_layout_relationship_bug',10,2); function toolset_fix_layout_relationship_bug($post_id, $form_data) { // one array for each form $hash = array( array( 'id' => 12345, 'slugs' => array('coordinator-educator'), ), ); // closure to find the matching array hash item and slug $find = function( $var ) use ($form_data) { $el = ($var['id']==$form_data['id']); return $el; }; // create an array of form IDs where you want to apply this code $forms = array_column( $hash, 'id' ); if ( in_array( $form_data['id'], $forms ) ) { // find the slug array in the matching hash item and use it to connect the parent post $thisHash = array_filter( $hash, $find ); $slugs = $thisHash[array_keys($thisHash)[0]]['slugs']; foreach( $slugs as $slug ) { $parent_id = $_POST['@' . $slug . '_parent']; $parent = toolset_connect_posts( $slug, $parent_id, $post_id ); } } }
Replace 12345 with the numeric ID of the new Child Form. The other ticket gives examples for extending this approach to multiple forms or to forms with multiple post relationships.
Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
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 3 replies, has 2 voices.
Last updated by 5 years, 9 months ago.
Assisted by: Christian Cox.