I'm using some code I found online to calculate the average value across all posts for [a_custom_field].
Does anyone know how to exclude (from calculation) any instances where the custom field = 0 or if the custom field has not been filled in? Or maybe there's a better method to get the same result.
global $wp_query;
$real_query = $wp_query->query; //save reall query
//create new query for all posts of the tag/category/whatever
$args = array_merge( $wp_query->query, array( 'posts_per_page' => '-1' ) );
$average_q = new WP_Query($args);
$price_count = 0;
$total_price = 0;
while ($average_q->have_posts()){
$average_q->the_post();
$total_price = $total_price + get_post_meta($post->ID,'60mins',true);
$price_count = $price_count + 1;}
wp_reset_query();
$average = $total_price / $price_count;
echo 'The average is' . round($average);
query_posts($real_query);
get_post_meta
return value and check it? – Dakshinamurthy Karra Jun 11 '12 at 4:25