WordPress.org

Ready to get started?Download WordPress

Forums

Sum value of custom fields only include fields with specified value (1 post)

  1. ebygomm
    Member
    Posted 3 years ago #

    Currently using the following code to return the total of all values recorded in the custom field called 'Distance'

    <?php
    
    $meta_key = 'Distance';//set this to appropriate custom field meta key
    $allkms=$wpdb->get_var($wpdb->prepare("SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
    echo '<p><img src="/wp-content/themes/voidy/img/Walking.png" style="margin:0px; padding: 0px;" align="middle" alt="Activity Icon" />'.$allkms . ' kms</p>';
    ?>

    I want to expand this to sum only those values where the post also has a value of 'Walking' in the custom field called 'Activity'

    Any help much appreciated

Topic Closed

This topic has been closed to new replies.

About this Topic