Home › Toolset Professional Support › [Resolved] creating post relationships automatically without using relationship field
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)
Tagged: Post-relationship
This topic contains 19 replies, has 2 voices.
Last updated by davelow 5 years, 12 months ago.
Assisted by: Minesh.
Tell us what you are trying to do?
I have CPTs (Breeds and Dogs) and have one-to-many relationship (Breeds Dogs) where Breeds is the parent.
Breed CPT does not, but Dog CPT will accept user posts via forms.
1) In creating the Dog form i could choose fields which include 'Breed'. I'm happy with this as I can make it mandatory. however, it alone does not create a post relationship.
2) If i wanted to establish a post relationship I could choose Relationship field (Breeds Dogs) however I cannot make it a required field.
Can I stick to (1) but use codes to establish the post relation based on the field 'breeds'
OR was there something I missed to make (2) a mandatory item before accepting the post?
Is there any documentation that you are following?
Is there a similar example that we can see?
What is the link to your site?
I left both options on the form work in progress.
hidden link
Hello. Thank you for contacting the Toolset support.
Well - do you want to assign a specific parent to parent field (do not allow user to modify) or you want to allow chose the specific parent?
Hi Minesh,
thank you for taking this up.
the user should be able to choose the specific parent
thanks,
ok and you want to make the parent field - required - correct?
yes that is correct. I'm able to do that now, except that the relationship does not establish automatically.
Ok I see two breed fields on your form, could you please tell me which field value you want to save to establish the relationship.
ok, I've removed the unwanted one. There should be only one field remaining.
Ok - you can use the post relationship API function toolset_connect_posts() with Toolset Froms hook cred_save_data.
Add following code to your current theme's funcitons.php file:
add_action('cred_save_data', 'cred_custom_callback_fn',10,2); function cred_custom_callback_fn($post_id, $form_data){ // if a specific form if ($form_data['id'] == 2364 ){ if (isset($_POST['wpcf-breed'])){ $parent_id = $_POST['wpcf-breed']; $parent = toolset_connect_posts( 'replace-relationship-slug', $parent_id, $post_id ); } } }
Where '
- 2364 is your Toolset form ID
- Replace replace-relationship-slug with your original post relationship slug.
More info:
=> 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
Hi Minesh,
Thank you for the suggested solution.
It does not seem to work as at the backend the posts are still not connected automatically.
vis-a-vis one that I have added from the admin. (dogsactually.com/community/trouble)
are my codes correct?
Ok - would you mind to share access details so I can check whats going wrong there.
*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
By checking the screenshots - the code looks almost OK but as you claim its not working I need to check this on your install to know why its not working.
Is it possible for you to send me access? If yes:
I have set the next reply to private which means only you and I have access to it.
well - I need FTP access details OR if you do not able to share it - remove the following code from your functions.php file.
//Custom Post title from Custom Field add_action('cred_save_data', 'custom_title',10,2); function custom_title($post_id, $form_data) { // if form "Join the Community" and name field is populated if ($form_data['id']==2364 && isset($_POST["wpcf-name"])) { //set title of post to value of "name" field $title = $_POST["wpcf-name"]; $my_post = array( 'ID' => $post_id, 'post_title' => $title, ); // Update the post into the database wp_update_post( $my_post ); } } add_action('cred_save_data', 'cred_custom_callback_fn',10,2); function cred_custom_callback_fn($post_id, $form_data){ // if a specific form if ($form_data['id'] == 2364 ){ if (isset($_POST['wpcf-breed'])){ $parent_id = $_POST['wpcf-breed']; $parent = toolset_connect_posts( 'replace-relationship-slug', $parent_id, $post_id ); } } }
Once you remove above code - let me know. I will take care of both the code.
thats done Minesh
Well - I see now why the post is not connected because you are using custom field breeds with static values:
=> hidden link
Now, the problem is you set the text value with each option "Custom field content" - it needs to be the same post ID of breeds post type.
So for example - for breed "Airedale Terrier" - you need to set ID 2381 for "Custom field content".
=> hidden link
I see that there is not all breeds added to this custom field - do you want dynamic way to add all breeds to the breed select box?
Is there a dynamic way? It will be very helpful as there are more breed posts coming.