Home Forums General Issues Autofill Custom Field With Average


Autofill Custom Field With Average

  • Hello. I have created a custom post type for sports players. I’m using ACF to insert a repeater field so that users can add game scores. Each row includes a date field, game type field and 10 fields for entering scores. I need two additional fields at the end of each row – one that calculates the average score of all 10 games and one that displays a rank of A, B or C depending on what the average and game type is. The average and the rank should be read only.

    I used the my_field.php file (taken from to create both the average and the ranking fields but am not sure where to place the code that calls the scores and displays this information.

    my create field functions look like this:

    function create_field( $field )
    		echo '<input type="text" id="' . $field['id'] . '" name="' . $field['name'] . '" value="' .$avgsc.'" readonly />';

    I tried assigning the average of the total combined custom field values for the game scores to $avgsc inside of this function but that didn’t work. Would the code for populating the average field go in this file?

    The other idea I had was to have the average and ranking fields autopopulate upon publishing or updating the post by inserting the code in my functions file, but not sure how to do that.

    Any help on how this can be accomplished would be great. Thanks!

  • Hi @cain04

    My first question is why do you want to calculate the value and then save it to the DB. Someone could then hack the data and change scores manually.

    It would be a much easier task to do the logic on the front end. Is this an option?

  • Hi

    Yes, that would be easier and I started to do it that way. However, as a player plays more days the system would then look at all of their past rankings and averages per game day to calculate an overall average and ranking.

    This should be how the player edit screen looks:

    Player Edit Screen

    Thanks much for a quick reply – totally love this plugin! 🙂

    BTW, while i filled in the fields in the screenshot in photoshop I did figure out how to create and populate the metabox for overall average and rank – however it’s only calculating total number of games and averages for a single game day instead of taking all averages from all game days played. Can’t get it to take into account all averages in repeater fields.

  • Hi @cain04

    I’m not 100% sure how you have setup the sub field for calculating the avg data, but this is how I would do it:

    1. Create a normal text field for ‘avg’ and ‘rank’.
    2. Use jQuery to calculate the score and update the value. Also, use JS to set the field as read only.

    In the docs, there is an article which coveres how to use the JS function ‘acf/setup_fields’ to run any JS. You would use this to trigger a function which loops over any rows in the repeater field and does the calculation.

    Shouldn’t take longer than 20 mins to write.

    If you are unable to write such code, perhaps you should think about contracting a JS developer to do so.

    Good luck,


Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Autofill Custom Field With Average’ is closed to new replies.