WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm asking for your help to develop a system average. I have a website video games and I created a function that automatically calculates the average for each review. It works very well. The problem is that I would like to display in my sidebar Rated 5 games. Unfortunately, I can not ask WP to calculate and compare each mean. It will take a lot of time. So I'd like to do is calculated at the publication of the review in the admin to save automatically mean a meta. I do not know anything about the possibility in the administration. Is it possible? If yes, how? thank you

/* RATE */

function get_rate_overall($rate=bool){

    $graphics = get_field('graphics');
    $gameplay = get_field('game-play');
    $life = get_field('life');
    $sound = get_field('sound');

    $overall = $graphics + $gameplay + $life + $sound;
    $overall = $overall / 4;
    if (is_float($overall)) { $overall = number_format($overall,1); }
    if ($rate == true) { echo $overall.'/10'; }
    if ($rate == false) { echo $overall; }  
share|improve this question
You need to show us some code. What have you tried? What are you having difficulty with? – chrisguitarguy Jan 7 '13 at 13:24
I have nothing except my writing function in the functions.php file: This feature allows me to display the average of a review. I want this operation to be performed at the time of publishing the article and recorded in a meta. I do not really know how to work or where to look. – Shingo Jan 7 '13 at 19:02
up vote 0 down vote accepted

You can accomplish this via the save_post action:

add_action( 'save_post', 'wpa78558_save_average_meta', 100 );

function wpa78558_save_average_meta( $post_id ) {

    if ( !wp_is_post_revision( $post_id ) ) {

        $graphics = get_field('graphics', $post_id);
        $gameplay = get_field('game-play', $post_id);
        $life = get_field('life', $post_id);
        $sound = get_field('sound', $post_id);

        $overall = $graphics + $gameplay + $life + $sound;
        $overall = $overall / 4;
        if (is_float($overall)) { $overall = number_format($overall,1); }

        update_post_meta($post_id, 'overall', $overall);



You can add additional checks in here for a specific post type (see example on save_post codex page), or verify that your field values exist before saving the meta data.

The average will be saved under the meta key overall and can be output in the template or referenced in a query.

share|improve this answer
Thank you for your help, I'll try and I'll let you know. thank you again – Shingo Jan 7 '13 at 19:47
Great, it works very well! Thank you again ! – Shingo Jan 7 '13 at 20:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.