Skip to content

Instantly share code, notes, and snippets.

@milohuang
Forked from banago/average.php
Created May 9, 2013 23:12
Show Gist options
  • Save milohuang/5551306 to your computer and use it in GitHub Desktop.
Save milohuang/5551306 to your computer and use it in GitHub Desktop.
<?php
/**
* Function: Average
* Calculates average of values taken from custom fields out of a WordPress loop. Built of Jason ;)
* Author: Baki Goxhaj
*/
function average( $post_no );
$no = 0;
$query = new WP_Query('posts_per_page=' . $post_no ); while( $query->have_posts() ) : $query->the_posts();
// Adding
$no = $no + get_post_meta( $post->ID, 'cutom_field_name', true );
endwhile;
//Calculate Average
$average = $no / $post_no;
echo $average
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment