• Hello everybody.
    I’m running a photo contest web, and the rules are:

    1. Each photo gets a score of each member jury, so each image gets 3 scores.
    2. The global score of the image is the addition of the 3 scores.
    3. The total score of the photographer is the addition of all the scores of all the images.

    Each image is in a different post, each member of the jury scores in a different custom field and I’ve managed to calculate the global score of the image by adding up the three custom fields and save it to another custom field of the post.

    But I’m trying to calculate the next step: add up all the scores of all the images of each user and save it to a meta value I’ve added for each user.
    I have tried the miles example of this page, but it gives me the total addition of all photos, not for every user: Codex

    I don’t know how to do it and I’m not sure if my english is god enough for you to understand.

    Anyone can help me? Thank a lo in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Gwythan

    (@kevin-ashbridge)

    I haven’t tested this, but you’ll need a MySQL statement something like this:

    [ Moderator note: Please wrap code snippets in the backtick character or use the code button. ]

    <?php
    // set the meta_key to the appropriate custom field meta key
    $meta_key = 'miles';
    $author_id = '1';
    $allmiles = $wpdb->get_var( $wpdb->prepare(
    	"
                  SELECT
                    Sum(meta_value)
                  FROM
                    $wpdb->postmeta
                 INNER JOIN $wpdb->posts ON $wpdb->postmeta->post_id = $wpdb->posts->ID
                 WHERE (meta_key = $s) AND (post_author = %s)
    
    	",
    	$meta_key, $author_id
    ) );
    echo "<p>Total miles is {$allmiles}</p>";
    ?>
    Thread Starter alexgagi

    (@alexgagi)

    Hi Kevin, thanks a lot for your help. This is to calculate for author 1. How do I do for every author?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to make an addition of a meta value for each user’ is closed to new replies.