Skip Navigation

[Resolved] Automatically set post parent in post form from URL parameter

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

Problem:
Automatically set post parent with Toolset CRED form using new post relationship

Solution:
You can use new post relationship API function 'toolset_connect_posts' in order to connect two posts in relationship of parent and child in conjunction with the use of cred_save_data hook.

For example:

add_action('cred_save_data','func_custom_post_title',15,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==243) {
         
        toolset_connect_posts('boat-safety-check',$_POST['@boat-safety-check_parent'], $post_id);
    }
}

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/automatically-set-post-parent-in-post-form-from-url-parameter/page/2/#post-902786

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

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.

This topic contains 26 replies, has 3 voices.

Last updated by Brad Tipper 3 years, 4 months ago.

Assigned support staff: Minesh.

Author
Posts
#902727

So i've just found this:

https://toolset.com/forums/topic/child-cred-form-on-parent-post/

Which sounds to me like it should do what I want. I've added that code, and the parent is correctly set in the CRED form, but when I submit the fom, there is no relationship saved?!?

#902750

Minesh
Supporter

Languages: English (English )

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

Well - Thanks for your confirmation.

I used following code to set your current post ID value for parent select dropdown:

		[cred_field field='@boat-safety-check.parent' select_text='--- not set ---' value="[wpv-post-id]" use_select2="never" class='form-control' output='bootstrap']

Could you please confirm it works for you.

#902764
2018-05-24 21_48_00-Edit Safety Check ‹ Flux — WordPress.png
2018-05-24 21_47_08-EXAMPLE BOAT _ Flux.png

Hmm, still doesnt work. Field is auto-populated correctly, but it doesn't save the association......

#902777

Minesh
Supporter

Languages: English (English )

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

Well - I would like to know here - have you migrated from the legacy post relationship with new many to many post relationship OR you just build everything with Toolset Beta plugins?

#902780

Fairly sure it's all built from scratch- that's what was suggested to me by support in the first place.

#902786

Minesh
Supporter

Languages: English (English )

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

Ok - the thing was that the API support for new relationship was not released yet but as it was a simple change, only 2 files needed to make this work. I've updated those required files to your types and CRED plugins.

However - we are going to release new version for all Toolset plugins by next week, its currently undergoing QA.

I've added following code to your current theme's functions.php file:

add_action('cred_save_data','func_custom_post_title',15,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==243) {
		
        toolset_connect_posts('boat-safety-check',$_POST['@boat-safety-check_parent'], $post_id);
    }
}

Now, I can see that once you add the new record - in backend its displayed as connected to selected boat. Could you please confirm.

#902791

OK, great! thanks heaps- appears to be working! OK, so do I need to remove that code once it's updated I assume? Will I get notified of the update in the WP dashboard or do I need to come back here to get the latest versions?

#902792

Minesh
Supporter

Languages: English (English )

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

Glad to know that its working fine now for you as well.

do I need to remove that code once it's updated I assume?
==> No - you need to keep the code I've added in your functions.php file.

Will I get notified of the update in the WP dashboard or do I need to come back here to get the latest versions?
==> Yes - once we release new version - you will see the notice on your plugins page to update plugins to latest version. However - please keep eye on our blog to have latest updates.
=> https://toolset.com/blog/

#902793

OK, so if I add another child post type i'm gonna have to add more if statements? That seems problematic- this all seems like a pretty basic function that should just work from the start?

#902799

Minesh
Supporter

Languages: English (English )

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

Yes - as you need to state to which post relationship you need to connect the post to - its obvious thing.

However - what you are saying, we are already on it and we have introduced post relationship forms - please check it out and try to play with it:
=> https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

We are about to release the new version and your comments are valuable for us. As your original issue is resolved, May I kindly ask you to open a new ticket with your each new question. This will help other users searching on the forum and help us to write correct resolve ticket summery.

Thank you for understanding. Glad to help. Have a nice day!

#903592

Minesh
Supporter

Languages: English (English )

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

Could you please kindly close this ticket.

#904121

Cheers, all good.