Skip Navigation

[Resolved] Post relationship is not saved correctly in Form

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

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 support ticket is created 5 years, 9 months 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)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#922059

Hi there,

I have a post type called coordinator. Coordinators are parents to educators. On the coordinator template, there is a view that lists the educators belonging to that coordinator. In this view, there are buttons that trigger modals containing the edit post form for the educator. There is also a form in a tab that allows for the creation of new educators.

The post relationship is set on the form using the [wpv-post-id] shortcode:

	<div class="form-group inviz">
		<label>Coordinators Educators</label>
		[cred_field field='@coordinator-educator.parent' value='[wpv-post-id]' class='form-control' output='bootstrap']
	</div>

When a new educator is created using the new post form, even though the relationship field is populated correctly with the parent, when submitted, the new educator is created with no parent.

If I remove the edit forms from the page by deleting the post form shortcode from the modal, the new post forms create posts with correct relationships.

I have no console warnings or errors and no php errors. Removing plugins and changing back to 2017 does not resolve the issue. This looks like a bug.

What do you think?

Thanks -
Mark.

#922205

Hi, is the Coordinator page designed using a Template Layout or Content Layout? There is currently a known issue where relationships are not being saved correctly when a new Child Post Form is shown on a page using a Layout. A code workaround is probably the most practical solution until we can get that bug addressed in Layouts and Forms. Another ticket here discusses the approach: https://toolset.com/forums/topic/adding-new-child-post-doesnt-assign-parent-while-editing-child-post-does/

Here's a snippet that you can add to your child theme's functions.php file:

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.

#922283

This worked - thanks.

#1189041

Quick update - this code workaround is no longer needed. The fix for this issue has been implemented in Forms and Layouts.

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