I am using the cred_save_data to insert rows in a custom table 1 that contains scores A, B and C along with id of the custom post type.
This happens every time a new post is created or an existing post is updated.
What I want to do maintain a running average of score A, score B and score C in a different custom table 2 so that I can easily reference it and show the 'progression' in average score A, B and C over time. Think of it like a historical record of the score over time.
I am easily able to insert data in custom table 1 using cred_save_date and decided to use cred_submit_complete to update data in table 2 thinking that it would give enough time for table 1 to be updated, which will enable accurate calculations for table 2.
Why would that matter? because table 2 calculations require the code to scan all records in table one and get an average for score A, B and C. If table 1 is not updated with score given in the post submitted using CRED by the time table 2 is updated, we would have the wrong average in the system.
And this is what is exactly happening. I am able to insert rows in table 2 using cred_submit_complete but it seems like table 1 does not get updated in the database by the time the cred_submit_complete hook is triggered.
I changed the priority of the hooks but nothing happened.
My questions: how can I ensure that table 2 is updated only after table 1 has been updated using cred_Save data. Is there a way to delay the cred_submit_complete hook or use some other hook that triggers only after table 1 has been updated?
Help appreciated.
I am able to insert rows in table 2 using cred_submit_complete but it seems like table 1 does not get updated in the database by the time the cred_submit_complete hook is triggered.
Hello, let me make sure I understand what's happening. It sounds like you have two Forms API hooks set up for the same Form submission - one hook using cred_save_data and another hook using cred_submit_complete. In the cred_submit_complete hook you are performing some calculations that should be influenced by the data stored during the cred_save_data hook. In your experience, the data stored in the cred_save_data hook is not influencing the data calculated during the cred_submit_complete hook. It seems that the data is not yet saved during the cred_submit_complete hook, and the calculations are therefore incorrect.
It's hard for me to know exactly what's happening here without seeing all the code, but I can give you some general information. In terms of the order of operations expected for the Forms API, the cred_save_data hook should always be triggered before the cred_submit_complete hook. You can test that by adding some error_log statements in both callbacks, for example:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
$forms = array( 123 );
if ( in_array( $form_data['id'], $forms ) )
{
error_log('save data');
// your other code here
}
}
add_action('cred_submit_complete', 'my_submit_data_action',10,2);
function my_submit_data_action($post_id, $form_data) {
$forms = array( 123 );
if ( in_array( $form_data['id'], $forms ) )
{
error_log('submit complete');
// your other code here
}
}
When submitting a Form with ID 123, this will produce the following log statements:
[27-Jan-2021 14:05:01 UTC] save data
[27-Jan-2021 14:05:01 UTC] submit complete
I suggest adding similar logs to your callbacks and testing the Form again. This test should confirm that the save data hook is fired before the submit complete hook, during the same form submission process. If you are not seeing this in your local tests, then I'll need to take a closer look. If you are seeing that the cred_save_data hook is fired before the cred_submit_complete hook as expected, then something else is going on. It could have something to do with the way your code inserts values into the database, or how it gets stored values from the database. This is especially important when related posts are involved, if post relationships are being set in the Form submission or in the custom code. The timing of that post relationship definition would be important.
Your understanding is correct and thanks for providing guidance steps. So it seems like the issue is not when the code is executed. The issues seems to be that data is not recovered by cred_submit_complete hook from the data inserted by cred_save_data on the same form.
This is what I have found:
1) I added a sleep (5) function to the top of cred_submit_complete to see if this would help but it did not. 5 secs is a long time and still the data was not retrieved from the table.
2) If I run the same query in mysql or I create a shortcode to extract the data and insert the shortcode on a page, I see the right values. So, the query seems to work but not inside the toolset custom code.
What is the query? It's pretty straightforward:
$expert_review_terms = get_the_terms(12637, "skill-category");
// this time loop expert review terms.
foreach ( $expert_review_terms as $term ) {
$sum_level_3_per_skill = $wpdb->get_results("SELECT SUM(skill_level_3_score) as value_3 FROM wp_skill_level_evaluations sle1 WHERE created_at = (SELECT MAX(created_at) FROM wp_skill_level_evaluations sle2 WHERE sle1.evaluation_post_id = sle2.evaluation_post_id) and term_slug = 'market-analysis'");
$count_level_3_non_null_entries_per_skill = $wpdb->get_results("select count( DISTINCT evaluation_post_id) as count_3 from wp_skill_level_evaluations where term_slug = 'market-analysis'");
if( $count_level_3_non_null_entries_per_skill[0]->count_3 != 0){
$level_3_average_score = round(($sum_level_3_per_skill[0]->value_3)/($count_level_3_non_null_entries_per_skill[0]->count_3));
}
else { $level_3_average_score = null;}
echo $level_3_average_score;
The $count_level_3_non_null_entries_per_skill variable returns zero in the cred_submit_complete hook but 1 in mysql and even in the shortcode. Only difference is the replacement of market analysis with term->slug.
So I found an error in my insert code as the term->slug was not under ''. 🙁
It's all good now. Sorted out. Thanks for your help.
My issue is resolved now. Thank you!