[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".
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).
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.
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: