Home › Toolset Professional Support › [Resolved] Split: Sum of Custom fields calculation – update parent post with average rating
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 is split from https://toolset.com/forums/topic/sum-of-custom-fields-calculation/
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: Content Templates, Views, Views plugin
Related documentation:
This topic contains 11 replies, has 2 voices.
Last updated by Minesh 4 years, 8 months ago.
Assisted by: Minesh.
I have one last question, which may be too difficult to accomplish... Given that this ratings system I created works so well I wanted to see if it was possible to take these averages and update the parents fields with the averages.
For example, in my CPT Destinations, I have the same custom fields as I do for ratings. The ratings are from users and the the fields for destinations are manually entered by me. Because these are 2 separate CPT's when creating a view to display all destinations with sorting controls etc. of course I can only use these sorting controls from what's entered in the database, not by the calculated averages, even though those can be be displayed within the view with a shortcode.
Do you think there is any possible way I can create a function to automatically update a given "destination's" values in the database with the current calculated averages when someone submits a cred form for a new rating of that destination?
Or possibly sort by this calculated average within Toolset views custom searches?
Either that or another way would save me from having to manually go through and edit each destination to keep the ratings up to date and to properly correspond with the calculations
Hello. Thank you for contacting the Toolset support.
I've split the ticket from your original ticket and handle the question here.
If you want to update anything on the fly once you submit the Toolset Form, you can use the Toolset Form API hook: cred_save_data or cred_submit_complete.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
Where within the above hook you should try to get the related parent using the post-relationship API function: toolset_get_related_post():
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
Once you get related parent post ID, based on the ID you should try to update your desired custom field value using the standard WP function: update_post_meta().
Please try the above suggestions and let me know if you require further assistance on this.
Well since I have the cred form on the page of the parent in the relationship, I should be able to query the post ID with container_id I've been testing it out and the cred form submits as normal and is properly storing data in the database but I think I'm doing something wrong with the update_post_meta function as it's not altering the data in the database.
here is the code I've come up with so far
add_action('cred_save_data', 'my_save_data_action',10,2); function my_save_data_action($post_id, $form_data, $container_id) { $meta_key = 'wpcf-quality-hotel-price'; $meta_value = my_average_func_ratings(); // if a specific form if ($form_data['id']==1912) { if (isset($_POST['wpcf-user-hotel-price'])) { update_post_meta( $post_id = '$container_id', $key = '$meta_key', $value = '$meta_value' ); } } }
Please let me know what you think I could do to achieve this, thanks!
So, if you confirm that container ID is your arent ID.
Then you should adjust the code as given under:
add_action('cred_save_data', 'my_save_data_action',10,2); function my_save_data_action($post_id, $form_data, $container_id) { $meta_key = 'wpcf-quality-hotel-price'; $meta_value = my_average_func_ratings(); // if a specific form if ($form_data['id']==1912) { if (isset($_POST['wpcf-user-hotel-price'])) { $post_id = $_POST['container_id']; update_post_meta( $post_id , $meta_key, $meta_value); } } }
where - Please adjust the container_id key with $_POST if required.
It's still not working for me, I've tried testing it by setting the id of the post and changing container_id key with $_POST but it doesn't seem to work
add_action('cred_save_data', 'my_save_data_action',10,2); function my_save_data_action($post_id, $form_data, $container_id) { $post_id = '2100'; $meta_key = 'wpcf-prices-score'; $meta_value = "22"; // if a specific form if ($form_data['id']==1912) { if (isset($_POST['wpcf-user-food-score'])) { update_post_meta( $post_id , $meta_key, $meta_value); } } }
let's just say the post ID I wanted to adjust was 2100 with a value of 22 and the meta_key was "wpcf-prices-score" I just wanted to see if it will work without getting the ID from the page. Any suggestions?
you should not use $container_id as the third argument of your function.
Can you please share problem URL and access details with me so I can fix it quickly on behalf of you. Please let me know where you added the Toolset Forms cred_save_data hook.
*** 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 have set the next reply to private which means only you and I have access to it.
Hey Minesh,
I got it to work, had to change it to
add_action('cred_submit_complete', 'my_success_action',10,2); function my_success_action($post_id, $form_data)
Not really sure why the original code wasn't working but by changing it to 'cred_submit_complete' it works great now. My last and only issue, because the function I made gets the ID for the post's metadata to be updated based on the pageID that the cred form is on and I want to keep it on there so the form doesn't have to be redirected to static page.
I'm sure this is the simplest solution of the all the questions I've asked, but the cred form I currently have on the parent page doesn't automatically show the parent post in the relationship. I know that users can simply select the parent, but this isn't a good UI and could really cause problems with the whole system if they select the wrong parent.
Again, the cred form is on the parent page so I feel like this must be a very simple fix, I just can't seem to figure it out. Thanks!
Not really sure why the original code wasn't working but by changing it to 'cred_submit_complete' it works great now.
=>
Great.
My last and only issue, because the function I made gets the ID for the post's metadata to be updated based on the pageID that the cred form is on and I want to keep it on there so the form doesn't have to be redirected to the static page.
I'm sure this is the simplest solution of the all the questions I've asked, but the cred form I currently have on the parent page doesn't automatically show the parent post in the relationship. I know that users can simply select the parent, but this isn't a good UI and could really cause problems with the whole system if they select the wrong parent.
==>
Do you mean that you added your form on the parent post? having said that the form to create a child post is added to your single parent post page? If you can share problem URL I can review it.
Got it working by adjusting the short code in the relationship field, hide to hide the selection with css though. On the topic of Cred forms, I notice there's not an option to validate numbers, as in set a maximum value. Since this could greatly alter my results, I really need to implement this. I've attempted using the form api hook cred_form_validate using this function but I'm not getting results. I've set it to if > 100 return error but even after testing with any number over 100, the form submits with no errors
add_filter('cred_form_validate','my_validation',10,2); function my_validation($error_fields, $form_data) { add_filter('cred_form_validate','my_validation',10,2); function my_validation($error_fields, $form_data) { //field data are field values and errors list($fields,$errors)=$error_fields; //uncomment this if you want to print the field values //print_r($fields); //validate if specific form if ($form_data['id']==12) { //check my_field value if ($fields['wpcf-my_field']['value']!='correct_value') { //set error message for my_field $errors['wpcf-my_field']='Wrong Value'; } //check if featured image exists if (empty($fields['_featured_image']['value'])) { //set error message for featured image $errors['_featured_image'] = 'Missing featured image'; } } //return result return array($fields,$errors); }
This isn't my exact code but it's what I used as a reference, where = I used is > than 100
Glad to know that you are able to implement the solution you were looking for.
Maybe CSS code is also not needed to hide as the option is you can hide/remove the parent select from your form and use the Toolset form API hook cred_submit_complete to connect your post.
I see now you want to validate some number but what number and against what field. I do not understand it. Additionally, May I kindly ask you to open a new ticket with your every new question you may have. This will help other users searching on the forum as well as help us to write correct problem resolution summery.
Sorry, I'll make sure to open a new ticket in the future.
So on the cred form I have, all the fields are number fields and I want to validate the form to not allow a user input of more than 100. Again the code I reference above is not my code I attempted, I just found that in the documentation and I believe it's the right function I'm looking.
Basically all I'm trying to achieve is have the form return an error if any of the fields has a number value of more than 100 .
Thanks for your assistance
Yes - you are on right track. You can use the cred_form_validate hook and add your validation code to compare your fields.
Please check the example code with the following related ticket - you need to adjust the code as per your custom requirement.
https://toolset.com/forums/topic/how-to-do-simple-validation/#post-379193