Skip Navigation

[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 support ticket is created 5 years, 8 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.

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 1 reply, has 2 voices.

Last updated by Minesh 5 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1082685
Unbenannt.JPG

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?
https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/

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.

#1082747

Minesh
Supporter

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

add_action('cred_save_data','func_connect_child_posts',15,2);
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);
    }
}

Where:
- 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='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-id]"
}
[/cred_generic_field]

Where:
- 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/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.