[Resolved] I need to automatically set the related post directly

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

Problem: I need to automatically set the related post directly

Solution: To dynamically connect the parent/child post using Toolset Form hook "cred_save_data" with post relationship API function "toolset_connect_posts".

You can find proposed solution, in this case, with the following reply: https://toolset.com/forums/topic/i-need-to-automatically-set-the-related-post-directly/#post-1082747

Relevant Documentation: => https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts => https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

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.

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 1 year, 7 months ago.

Assigned support staff: Minesh.


Tell us what you are trying to do?
We have a post type (e.g.) Diary. The Diary Single pages have forms on them to allow Users to make Diary Entries (Second Custom Post Type, "one to many" relation).
If the user is on the Diary Page (parent) he must be able to create entries that are automatically related to the parent type (without the need or possibility to select other connections).

Is there any documentation that you are following?

The Problem:
1) I don't really get out of the tutorial how the field is populated.
2) It must be something like hiding the field but pasting the actual Diary ID automatically into the value value="" part of the drop down? But this seems not to work.
3) We will not be able to use the title (it seems the manual Dropdown works that way) because the entries are user generated so its perfectly possible that 2 posts of different users will have exactly the same title.

4) No core priority - yet - but I wondered If just hiding the dropdown via "display: none" would mean that everyone with rudimentary CSS skills could "unhide" it and mess things up by posting into the diaries of other users which would be a vulnerability.

Thank you very much.



Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - You should add the following code to your current theme's functions.php file to dynamically connect the parent/child post using Toolset Form hook cred_save_data with post relationship API function toolset_connect_posts

function func_connect_child_posts($post_id,$form_data) {
    if ($form_data['id']==999999) {
        toolset_connect_posts('relationship-slug',$_POST['@relationship-slug.parent'], $post_id);

- Replace relationship-slug with your original relationship slug
- Replace 999999 with your original form ID

Edit your form and remove the parent field if its already added to form and add Generic hidden filed which holds the parent post ID as default value where the review form is display currently , as given under:

[cred_generic_field field='@relationship-slug.parent' type='hidden' class='' urlparam='']

- Replace relationship-slug with your original relationship slug

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
=> https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/