Home Forums General Issues Calculation…



  • I’m creating an athlete profile website. I’m using ACF to create custom fields. Each athlete profile will have various stats. For example, let’s say one of the fields is weight. One athlete might be 6 feet and another is 6 feet 4 inches. Let’s also say there are 1000 athlete profiles. I want to create an automatically calculated field (read only) on the athlete profile that looks at all of the athlete profile height record and does a simple calculation to determine where that athlete compares to all of the other athletes. For example, maybe the 6 feet 4 inch athlete is in the top 94% of everyone in the database. It’s the number “94” I’m looking for. Is that possible to do with ACF? Does what I’m wanting to do make sense?

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

  • How much time would be involved? And would the value that’s generated be something that can by dynamically inserted into a Divi module?

  • Using divi, I have no idea how you would do this. More than likely you’d have to build a custom shortcode for every value you want to display, but that’s just a guess, I do not use divi, but I do know that with most of these page builders it is the only practical way to do something like this.

    How long it would take you would depend on the number of statistics you want to do this for.

    The first thing you would need it to create an acf/save_post action that will put all of the statistics every time a post is added or updated and store all statistics for all posts in a single array in the options table. This would have to be done to prevent the entire thing from timing out (crashing) the site. Depending on the number of values you are saving this could take 1 to several hours.

    The next thing you’d need to do is build a custom shortcode for each statistic. There are plenty of tutorials available on the web for doing this as well. The first one might take you a couple of hours to build, after that you could likely copy and past the first one and then change the name and modify the array indexes used to do the calculations and then test. You’re probably looking at an average an hour per statistic, maybe a little less once you get started depending on how many there are. You might also be able to use a function to do the actual calculation based on arguments and reduce the time taken to build new statistic shortcodes and the amount of code needed. This is what I’d probably do, so you might be looking at 2 hours to build the first one + the function and then 30 minutes or so for each additional shortcode.

  • Saving field values into an array in the options table, actually, this is the easy bit.

    add_action('acf/save_post', 'my_save_statistics');
    function my_save_statistics($post_id) {
      // array holding list of fields to save to options array
      fields = array(
      $all_stats = get_options('all_stats', array());
      $post_stats = array();
      foreach ($fields as $field) {
        $post_stats[$field] = get_field($field, $post_id);
      $all_stats[$post_id] = $post_stats;
      update_options('all_stats', $all_stats, true);
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.