Skip Navigation

[Resolved] Disconnect specified parent in child edit form. Use generic and/or select field?

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 4 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2705749

Hi there

I have a many to many relationship between 2 custom posts and would like the user to be able to edit the child post to:
1. automatically disconnect the relationship with the current parent post
2. set the relationship with another parent post

So far I have a function working to 'set another parent post' using a generic field but I can't figure out how to disconnect the 'current parent post'.

I used the principles of this post for the 'set another parent post' function:
https://toolset.com/forums/topic/how-to-pre-select-more-than-one-parent-on-a-child-post-submission-form/

Here's what I now have:

Child post edit form
The form uses a generic multiselect field with four views. Two views specify the id of the parent post to be ATTACHED as an id number and as a select field option. Two views specify the id of the parent post to be DISCONNECTED as an id number and as a select field option.

[cred_generic_field type='select' field='mygenericfieldslug1']
{
"required":1,
"persist":1,
"default":[ [wpv-view name="disconnect-this-parent-post-id"] ],
"options":[ [wpv-view name="disconnect-this-parent-post-generic-field-options"] ]
}
[/cred_generic_field]

[cred_generic_field type='multiselect' field='mygenericfieldslug2']
{
"required":1,
"persist":1,
"default":[ [wpv-view name="set-this-parent-post-id"] ],
"options":[ [wpv-view name="set-this-parent-post-generic-field-options"] ]
}
[/cred_generic_field]

Function to set the new parent post
This is the function in functions.php that successfully sets the relationship with the new parent post using the "mygenericfieldslug2" generic field from the form.

add_action('cred_save_data', 'ff_set_related_folder',10,2);
function ff_set_related_folder($post_id, $form_data) {
  $forms = array( 888888 );
  $relationship_1_slug = "parent-child-relationship";
  $ms_field_slug = "mygenericfieldslug2";
  if ( in_array( $form_data['id'], $forms ) )
  {
    $ms_selections = $_POST[$ms_field_slug];
    foreach($ms_selections as $ms_selection) {
      toolset_connect_posts( $relationship_1_slug, $ms_selection, $post_id);
    }
  }
}

So now the problem is how would I tweak that function to DISCONNECT (rather than connect) the specified parent id from the "mygenericfieldslug1" generic field in the child form.

I have tried so many ideas my brain is swirling...

Hoping this is possible!
Thanks in advance.
Rita

#2705772

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset Forms offers shortcode [cred-delete-relationship] that you should use to delete the relationship.
- https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred-delete-relationship

#2705779

Hi Minesh
Yes, I am aware of the delete-relationship shortcode. I use it elsewhere on the site... But I don't see how it is useful in this scenario as the shortcode would need to be inserted into a seperate view as a seperate task for the user. In this case, I want to be able to 'swap' the parent post ie disconnect one relationship and assign another, automatically and all in the same - one click - child post cred form...
R

#2705780

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

In that case if you want to disconnect the post in relationship using PHP what if you try to use the post-relationship API function: toolset_disconnect_posts()
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_disconnect_posts

For example:

// Disconnect a custom Author post with the ID of "5" from a custom Book post with the ID of "7" in a post relationship between Books and Authors
toolset_disconnect_posts( 'book-author', 5, 7 );

You will have to just replace the parent and child IDs using your desired parent and child post variable that holds the ID.

#2705802

Oh! I didn't realise there was that function. Great! Problem solved. Thankyou!