The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

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

  1. ebygomm
    Posted 5 years ago #

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

    $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