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