Home Forums General Issues Calculation… Reply To: Calculation…

  • This is not something I would do with an ACF field or something that I would store anywhere. These are calculations that I would do only when the value needs to be displayed. The reason you would not store it is that every time a post is added or a post is updated the value of each calculated field for every post would need to be recalculated.

    To do the calculation on the fly using only what is provided in WP and ACF and not adding a lot of extra coding work would mean doing a query to get all the posts (athletes), looping over them and getting the field values, adding them up and then dividing by the total number of posts and then comparing. This would have to be done every time. This would create a significant performance issue if you tried doing it on multiple values. With 1000+ posts this would more than likely time out the loading of a page.

    If you want to have this is a field then it would need to be done every time the profile is loaded. If you want the value to update when a field is changed then it would mean adding ajax to do the calculations every time a field is changed.

    To improve performance you could create a WP Option that holds the values for every athlete in an array. This option would be updated whenever an athlete is added/updated/deleted. Again, if you want to have anything happen when a value is changed instead of after saving you would need to add AJAX that updates the option value and performs the calculation.

    Overall, doing something like this with all statistics for each athlete would be a large project, even if you exclude the AJAX.