Skip Navigation

[Resolved] Calculating sum of fields from related posts

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 contains 0 replies, has 1 voice.

Last updated by davidL-7 1 month, 2 weeks ago.

Author
Posts
#2800694
Screenshot 2025-03-13 at 5.08.53 PM.png
Screenshot 2025-03-13 at 5.07.52 PM.png

Tell us what you are trying to do?

I need to calculate the sum of a set of fields from a series of related posts. Here's a simple breakdown of the challenge:

The page (hidden link) displays a view ("Account Summary by Year") that queries the custom post type Aid Requests, filtering them to show only the Aid Requests that match a particular year and Aid Account (filtered by the query string).

For each resulting Aid Request, I need to calculate the amount of that request. This is tricky because each Aid Request is composed of a set of Aid Items (related post types, one Aid Request to many Aid Items). Each Aid Item has a quantity and cost.

So in plain English, the formula for calculating the amount of each Aid Request would look like this:

1) For each Aid Request, find the related Aid Items.
2) Loop through each of those Aid Items, and multiply the cost times the quantity for the total cost.
3) Add up the total cost of all of the Aid Items

I believe I probably need to achieve this via a custom function, but am not sure how to go about it.

Is there any documentation that you are following?
Is there a similar example that we can see?

No, but Minesh helped me solve a related problem in calculating the sum for a single Aid Request. Here's an example of the end product of that work: hidden link The Requested Items are in a view named List Aid Items for Current Aid Request. The view uses Settings > Custom Code "calculate_sum" which does this task for the Aid Items belonging to a single Aid Request.

The difference with this request is that I need it to loop through and calculate for a whole series of Aid Requests.

In case the above is not clear, I'm attaching two screenshots. The first is from my work-in-progress, with the part I'm needing to calculate highlighted in yellow. The second is from the legacy app we're rebuilding with Toolset, and that one shows what the calculated amounts should be.

What is the link to your site?
hidden link
(By the way, Minesh should still have access to WordPress for this site, as I haven't changed the login info after our last support request).

Thanks!
David

#2800709

I created a separate view to return each Aid Request, then used the function for calculating the aid items that Minesh had written for me (thank you!) . I was able to pull the number produced by the view into the view used on this page, using a shortcode attribute to filter the view each time, like so:

[wpv-view name="sum-cost-of-aid-items-for-specified-aid-request" postid="[wpv-post-id]"]