Skip Navigation

[Resolved] Save Child Post Field Average and Count to Parent Post 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 6 replies, has 2 voices.

Last updated by Minesh 1 year, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2633845

I have a custom post type of "company" with a child custom post type of "company-review".

The "company" post type has 2 fields of "company-ratings-average" and "company-ratings-total"

The "company-review" custom post type has a custom field of "company-rating".

There is a cred form with ID 722. That cred form is on the company post type page and when a user fills out that cred form it creates a "company-review" child post attached to the company post. The cred form has the field of "company-rating".

What I want is when a user fills out that cred form it populates the "company-ratings-average" field of the "company" with the average of all the "company-rating" fields and the "company-ratings-total" with the total number of company-review child posts.

I am following along with this post: https://toolset.com/forums/topic/sort-by-highest-rated/

Here is my code:

function update_company_review($post_id, $form_data) {
// if specific form, change ID to the CRED "Review" ID
if ($form_data['id'] == 722) {
// Get ID of Company Being Reviewed
$parent_post = $_POST['@company-company-review_parent'];

$args = array(
'posts_per_page' => -1,
'post_type' => 'company-review',
'meta_key' => '@company-company-review_parent',
'meta_value' => $parent_post,
);
$reviews = get_posts($args);

$sum = 0;
$num = 0;
foreach ($reviews as $review) {
$ratings = get_post_meta($review->ID, 'wpcf-company-rating', true);
if ($ratings) {
$sum += $ratings;
$num++;
}
}
$average = ($num > 0) ? $sum / $num : 0;

// You can keep or adjust the rounding logic as needed
$res = round($average * 2) / 2; // Rounds to nearest 0.5

update_post_meta($parent_post, 'wpcf-company-ratings-average', $res);
update_post_meta($parent_post, 'wpcf-company-ratings-total', $num);
}
}
add_action('cred_save_data', 'update_company_review', 10, 2);

My issue is that the "company-ratings-average" and "company-ratings-total" fields just end up being 0.

Any help is appreciated and let me know if you need more info.

#2633923

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To get the related child posts, you can use the post relationship API function: toolset_get_related_posts
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

What if you try to use the following code:

function update_company_review($post_id, $form_data) {
// if specific form, change ID to the CRED "Review" ID
if ($form_data['id'] == 722) {
// Get ID of Company Being Reviewed
$parent_post = $_POST['@company-company-review_parent'];


$reviews = toolset_get_related_posts($parent_post,'company-review','parent',999,0,array(),'post_object','child');

$sum = 0;
$num = 0;
foreach ($reviews as $review) {
$ratings = get_post_meta($review->ID, 'wpcf-company-rating', true);
if ($ratings) {
$sum += $ratings;
$num++;
}
}
$average = ($num > 0) ? $sum / $num : 0;

// You can keep or adjust the rounding logic as needed
$res = round($average * 2) / 2; // Rounds to nearest 0.5

update_post_meta($parent_post, 'wpcf-company-ratings-average', $res);
update_post_meta($parent_post, 'wpcf-company-ratings-total', $num);
}
}
add_action('cred_save_data', 'update_company_review', 10, 2);

If above code does not help then I will require problem URL where you added the form as well as the admin access details and also share where you added the code you shared.

#2634103

Just tried this code and it is still showing 0's for both fields. Please request a private message so I can send you login and other info.

#2634343

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please send me admin access details as well as the problem URL where I can see the form and where you added the code you shared.

*** 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.

#2634743

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now:

I've adjusted the hook as given under:


function update_company_review($post_id, $form_data) {
// if specific form, change ID to the CRED "Review" ID
if ($form_data['id'] == 722) {
// Get ID of Company Being Reviewed
$parent_post = $_POST['@company-company-review_parent'];
 
$reviews = toolset_get_related_posts($parent_post,'company-company-review','parent',999,0,array(),'post_object','child');
	
$sum = 0;
$num = 0;
foreach ($reviews as $review) {
$ratings = get_post_meta($review->ID, 'wpcf-company-rating', true);
if ($ratings) {
$sum += $ratings;
$num++;
}
}
$average = ($num > 0) ? $sum / $num : 0;
 
// You can keep or adjust the rounding logic as needed
$res = round($average * 2) / 2; // Rounds to nearest 0.5
 
update_post_meta($parent_post, 'wpcf-company-ratings-average', $res);
update_post_meta($parent_post, 'wpcf-company-ratings-total', $num);
}
}
add_action('cred_submit_complete', 'update_company_review', 10, 2);

There were issue with post relationship slug. I've adjusted the correct slug: company-company-review

Also, we are using the Form's hook "cred_submit_complete". Can you please confirm it works as expected now.

More info:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

#2634881

That works great! Thank You! I just realized that the edit form needs to update the 2 fields as well. Would you mind updating the code to add form ID: 783 which is the "Company Review Form Edit"? It uses the exact same fields.

#2634981

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to use the following code:

function update_company_review($post_id, $form_data) {
// if specific form, change ID to the CRED "Review" ID

$allowed_form_ids = array(722,783);

if (in_array($form_data['id'],$allowed_form_ids) ) {
// Get ID of Company Being Reviewed
$parent_post = $_POST['@company-company-review_parent'];
  
$reviews = toolset_get_related_posts($parent_post,'company-company-review','parent',999,0,array(),'post_object','child');
     
$sum = 0;
$num = 0;
foreach ($reviews as $review) {
$ratings = get_post_meta($review->ID, 'wpcf-company-rating', true);
if ($ratings) {
$sum += $ratings;
$num++;
}
}
$average = ($num > 0) ? $sum / $num : 0;
  
// You can keep or adjust the rounding logic as needed
$res = round($average * 2) / 2; // Rounds to nearest 0.5
  
update_post_meta($parent_post, 'wpcf-company-ratings-average', $res);
update_post_meta($parent_post, 'wpcf-company-ratings-total', $num);
}
}
add_action('cred_submit_complete', 'update_company_review', 10, 2);

Can you please confirm it works as expected.

#2635435

That works perfectly. Thank You for your time and help. Have a great rest of your day.