Skip Navigation

[Resolved] Difficulty Storing Related Post

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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 47 replies, has 4 voices.

Last updated by Shane 5 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#908451

Great, hopefully we will have the issue resolved very soon and this workaround will not be needed. I will keep you posted here.

#910867

Hi Christian,

I've just noticed another one of my forms (very similar setup) is not storing the relationship even when having the status set to publish. I wonder if I'm doing anything wrong?

	<div class="hidden">
		[cred_field field='@product-listing_product-review.parent' select_text='--- not set ---' class='hidden' output='bootstrap' urlparam='productid']
		[cred_field field='@household-child_product-review.parent' select_text='--- not set ---' class='hidden' output='bootstrap' urlparam='childid']
	</div>

	[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']
#911424
temp.jpg

Hi Christian,

I'm really starting to PANIC here. It seems none of my forms are saving relationships even if set to publish (not draft). I'm set to launch the website in 9 days and basically all the functionality is shot without being able to save relationships between posts. I'm in serious need of getting this figured out.

To give you an example, the code below produces the attached screenshot but when submitting the form it creates the post with NO connection. Hoping for some quick help. Am I missing something obvious?

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']

	<div class="form-group">
		<label>Term Name (i.e. "Spring 2018")</label>
		[cred_field field='post_title' post='household-term' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Term End Date</label>
		[cred_field field='household-term-end-date' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		[cred_field field='@household-household-term.parent' urlparam='household' select_text='--- not set ---' class='form-control' output='bootstrap']
	</div>

	[cred_field field='form_submit' value='Submit' urlparam='household' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]
#911506

I don't see anything obviously wrong with this code or this form. Can you give me more information?
- Please provide step-by-step instructions how to get to this form on your site.
- Were you testing using the URL parameter, or manually selecting the parent post in the relationship field?
- I see in wp-admin there is currently a parent Household for the Household Term Fall 2018. Was this added manually?
hidden link

#911517

Hi Christian,

I had to manually link the posts to continue testing functionality. I just linked you to my account so, in theory, that should grant you front-end access to my homeschool and you should see what I see. Here's what you should do:

1) Click on Be.Toolset > My Homeschool
2) Click Add a Term
3) Put in some fake details - I put "Test Term" and 22 December 2018 as the end date.
4) The homeschool should be auto-selected "McKeon Family Homeschool" (which will normally be hidden once this is fixed).
5) It should bring you to the Term page with a success message, but you'll notice "Go Back to..." isn't complete because it doesn't have a parent post type.
6) Navigate your way back to the My Homeschool page and you won't see term you just added.
7) Go to the back end and you'll see it there under "Homeschool Terms" with no parent post type associated.

Thanks for looking into it.

- Aaron

#911824

Okay our developers just released Types 3.0.1, which includes several fixes for post relationship connection issues. May I request that you download the latest version of Types from https://toolset.com/account/downloads, then retest? This will help me figure out what direction to take to get this resolved completely.

#911835

Hi Christian,

I updated to Types 3.1 and still no luck.

- Aaron

#911942

Okay thanks, I see that the published/pending child status issue has been resolved, but another issue related to Forms and Layouts is still not resolved. If a template Layout is used to display the child post Form, then the parent post does not get set correctly. I'll update you when I have some additional information to share. If necessary, we can look into custom cred_save_data callbacks that manually set the post relationship values to keep your site development progressing.

#913020

Hi Christian,

Just checking in on this. I think my plan should be that by the end of the day Friday I need to either have the issue resolved or some workaround code in place. I need the weekend to make final wrap-ups on the site, load content, etc., so the major bugs should be resolved before then.

If we go the workaround code route, it'd be nice if the code could be usable on several forms so as to avoid writing separate custom code for each post-form / post-relationship. Maybe it becomes an if/then based on form ID that sets a variable with the relationship slug and then have it run the code to update the appropriate tables?

- Aaron

#913509

I don't see that the layout and post relationship issue will be resolved by Friday, so I think the workaround cred_save_data hook will be necessary. Please test this extensible function you can use for multiple forms:

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' => 198,
      'slug' => 'household_household-term',
    ),
    array(
      'id' => 6,
      'slug' => 'book-chapter',
    ),
  );
  
  // 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 of the matching hash item and use it to connect the parent post
    $thisHash = array_filter( $hash, $find );
    $slug = $thisHash[array_keys($thisHash)[0]]['slug'];
    $parent_id = $_POST['@' . $slug . '_parent'];
    $parent = toolset_connect_posts( $slug, $parent_id, $post_id );
  }
}

You will modify the $hash variable to include an array for each Form where you want to fix the relationship bug. You will use the numeric ID of the Form, and the correct relationship slug.

#913569

Hi Christian,

Thanks. I just activated the code snippet and can confirm it works on form 198. I will start fleshing out the array with all the form ID's and relationship slugs and report back if there are any issues, but I think this should get me by until a permanent fix can be put in place.

- Aaron

#913579

Question - what if one of my forms uses two relationships?

- Aaron

#913583

I can confirm the code seems to be working on multiple forms now (although I have not yet tested all 14 forms. The only thing I'm not sure about is how it will handle forms where there are two relationships. There are 4 forms that fall into this category. On reading the snippet, I'm guessing it will find two entries in the array but since there is no loop I'm thinking it won't process both of them? Let me know what you think. Thanks.

- Aaron

#913651

Okay yes, that will require a refactor to use an array of slugs for each form, something like this:

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' => 198,
      'slugs' => array('household_household-term', 'household_household-member'),
    ),
    array(
      'id' => 6,
      'slugs' => array('book-chapter'),
    ),
  );

  // 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 );
    }
  }
}
#913657

Updated the snippet and now the relationship doesn't seem to be saving. Tested it on form 198 again. Confirmed the snippet runs.

- Aaron