WordPress.org

Forums

Add and Average Custom Fields (2 posts)

  1. slicesofamerica
    Member
    Posted 1 year ago #

    I'm creating a music review website. For each album, there are 7 criteria, each set up as its own custom field. How can I add these and divide by 7 to get the average? I would also like to display the average. Don't know where to begin! I am currently doing the math by hand.

    http://taylorlasseigne.com/albums/atlas/

  2. catacaustic
    very awesome
    Posted 1 year ago #

    If these are held in custom fields, they are most likely stord in the wp_postmeta table and are related to the post ID. You'll need to know the name of each one, and you'll be able to do this. As a pure example without knowing what the fields are called...

    This is a slightly innefficient way of doing it, but it pretty much explains what you'll need to add into your code.

    $criteria_1 = get_post_meta ($post->ID, 'criteria_1', true);
    $criteria_2 = get_post_meta ($post->ID, 'criteria_2', true);
    $criteria_3 = get_post_meta ($post->ID, 'criteria_3', true);
    $criteria_4 = get_post_meta ($post->ID, 'criteria_4', true);
    $criteria_5 = get_post_meta ($post->ID, 'criteria_5', true);
    $criteria_6 = get_post_meta ($post->ID, 'criteria_6', true);
    $criteria_7 = get_post_meta ($post->ID, 'criteria_7', true);
    
    $average = ceil ($criteria_1 + $criteria_2 + $criteria_3 + $criteria_4 + $criteria_5 + $criteria_6 + $criteria_7) / 7;

Topic Closed

This topic has been closed to new replies.

About this Topic