Skip Navigation

[Resolved] Adding new child post doesn’t assign parent, while editing child post does

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

Problem: It seems that a new Child Form on the parent post page does not properly assign the parent post. The parent appears to be selected, but not saved.

Solution: There is currently a known issue where new child post forms displayed on a page with a Layout are not assigning parents correctly. The following code snippet will work around the issue until a fix is ready:

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' => 282,
      'slugs' => array('profile-service'),
    ),
  );
     
// 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 );
    }
  }
}
This support ticket is created 6 years, 6 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 6 replies, has 2 voices.

Last updated by PiotrO586 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#916997

Hi,

I have Types Forms for adding and editing child posts. The forms have a field for choosing parent post.
The problem is that when I use the form for adding a new child post, the parent, although chosen, is not saved (assigned) to the child post. When I, however, edit the newly added child post and then choose to which parent it should be assigned, the parent is saved. I use the newest Toolset version.

#917164

Hi, can you tell me if the New Child Post Form is displayed on a page with Layout? There is currently an issue with automatically selecting the correct parent post ID when a Form is displayed in a Layout. One workaround is to disable the Layout for the page containing the Form, another workaround is a custom code snippet. I can help you implement the custom code if necessary. I need to know the relationship slug or slugs and Form IDs to provide the proper snippet.

#917165

Hi Christian,

The form is called with [cred_form form="x"] shortcode inserted directly into the page editor, but the page itself has a layout assigned (as layout being used as template for post type). There are several forms, so maybe I will provide form ID and relationship slug just for one form, and I'll try to adjust your code for another forms?

Form ID: 282
Relationship slug: profile-service

As I understand this code would be temporary until some patch is released?

#917518

Okay yes, it's a temporary solution that can be removed once the patch has been released. Here is the code:

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' => 282,
      'slugs' => array('profile-service'),
    ),
  );
    
// 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 );
    }
  }
}

To extend this to another form or forms, you will add new array elements to the $hash array. Each array should contain the Form ID and the post relationship slug or slugs as an array. For example, if you need to add another Form with ID 4567 and the relationship slug 'book-chapter', the code will look like this:

  $hash = array(
    array(
      'id' => 282,
      'slugs' => array('profile-service'),
    ),
    array(
      'id' => 4567,
      'slugs' => array('book-chapter'),
    ),
  );

If the Form contains parent selectors from multiple post types, the code will look like this:

  $hash = array(
    array(
      'id' => 282,
      'slugs' => array('profile-service'),
    ),
    array(
      'id' => 4567,
      'slugs' => array('book-chapter', 'volume-chapter', 'section-chapter'),
    ),
  );
#920402

Hi Christian,

sorry for late response, I had another issue with my website and I couldn't check out your code earlier.
Anyway, your solution works. Thank you!

#1189042

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

#1190441

Hi Christian, ok, thanks for your note.