Skip Navigation

[Resolved] Create child with cred form, assigning it to correct parent given by view-item

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

Problem:
Create child with cred form, assigning it to correct parent given by view-item

Solution:
You can use the Form's API hook "cred_save_data" in order to perform any customization after you save the post and to connect the post, you can use the post-relationship API function: toolset_connect_posts

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/create-child-with-cred-form-assigning-it-to-correct-parent-given-by-view-item/#post-1514511

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 4 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 5 replies, has 2 voices.

Last updated by Andreas 4 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1512715
cluster-with-toolset.JPG
cluster-with-toolset-new-post.JPG

Hi there,
I have two CPT: "Custom Cluster" and "Custom Cluster Post", where "Custom Cluster" is parent of "Custom Cluster Post".
I am displaying the nested list with a child-view inside the parent-view as described here: https://toolset.com/documentation/user-guides/views/using-a-child-view-in-a-taxonomy-view-layout/
You can see the situation in the attached screenshot.

What I am trying to do:
I want the user to add a new "Custom Cluster Post" (child) for any "Custom Cluster" (parent) with a Cred form and have the new child automatically assigned the relationship with its correct parent, which is determined by the corresponding item of the parent-view.
I am aware of this solution: https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/ (hiding the pre-selected option with css).
But since I am not on a single parent-page, but displaying all the parents within a view on the same page, this does not work. There is no option for the parent pre-selected (see screenshot).

I searched through the forum, but couldn't find a solution for this situation, pulling the correct parent from the view-item.
Any help would be very appreciated. Thanks!
Regards, Andreas

#1512865

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please send me a problem URL where you are listing all the clusters and its associated child posts.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1512895

Minesh
Supporter

Languages: English (English )

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

Thank you for sharing the access details.

I see within your view you added the following shortcode:

 [fusion_modal_text_link name="create-new-custom-cluster-post" class="" id=""]Add new post[/fusion_modal_text_link]

I'm not sure how you link the Toolset form to open with this shortcode. Can you please tell me how you link the Toolset form to open with the above shortcode?

#1512983

Hi Minesh,
thank you for your reply.
This is a Avada shortcode, opening the form in a modal window. I removed this and put the Toolset form-shortcode "[cred_form form="create-custom-cluster-post"] " now directly into the view's template.

#1514511

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've removed the parent field dropdown from the child form:
=> hidden link

<div class="form-group">
		<label>Custom Clusters - Custom Cluster Posts</label>
		[cred_field field='@custom-cluster-custom-cluster-post.parent' class='form-control' output='bootstrap' value="[wpv-post-id]" use_select2="never" select_text='--- not set ---']
	</div>

And added the following generic field that will hold the parent field ID:

[cred_generic_field type='hidden' field='related_parent_post_id']
{
"default":"[wpv-post-id]"
}
[/cred_generic_field]

Now, to connect the child post with the parent:
- we need to use the Toolset Form's API hook: cred_save_data
- post relationship API function: toolset_connect_posts() to connect the post.

To do that, I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

add_action('cred_save_data','func_connect_parent_to_child',15,3);
function func_connect_parent_to_child($post_id,$form_data) {
    if ($form_data['id']==441) {
          toolset_connect_posts('custom-cluster-custom-cluster-post',$_POST['related_parent_post_id'], $post_id);
    }
}

I can see when you add the child post title with the following link: hidden link
The newly created child post will be connected automatically to the correct parent from where you created the child post.

I hope the solution I shared helps you to resolve your issue.

#1514595

My issue is resolved now. Thank you!