Skip to content

Instantly share code, notes, and snippets.

@banago
Created March 4, 2012 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save banago/1971523 to your computer and use it in GitHub Desktop.
Save banago/1971523 to your computer and use it in GitHub Desktop.
Calculates average of values taken from custom fields out of a WordPress loop. Built of Jason ;)
<?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