WordPress.org

Support

Support » How-To and Troubleshooting » Sum value of custom fields only include fields with specified value

Sum value of custom fields only include fields with specified value

  • 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

  • The topic ‘Sum value of custom fields only include fields with specified value’ is closed to new replies.