get('evp'); if ( is_numeric($evp) && $evp == $post->ID ){ $evp_m = (int) get_post_meta($evp, 'evo_click', true); $evp_m++; if (!update_post_meta($evp, 'evo_click', $evp_m)){ add_post_meta($evp, 'evo_click', $evp_m, true); } } } /** * Add the variable to identify the clicked link * Note: assumes using pretty permalinks * @param String $link the permalink * @param Object $post post item * @return String altered permalink */ function add_evo_click_vars($link , $post){ $link .= '?evp='.$post->ID; return $link; } function return_posts(){ $post_num = get_option('posts_per_page'); //get double the number of posts asked for //get them in a random order $post_limit = $post_num * 2; $the_posts = get_posts('numberposts='.$post_limit.'&orderby=date'); //loop through the posts to get their weightings $weight_array = array(); $id_array = array(); //get the ID and thr weighting for each post $pcount = 1; foreach( $the_posts as $post ){ //get the number of clicks $weight = (int) get_post_meta($post->ID, 'evo_click', true); if ($pcount==1){ $weight = $weight + $this->boost_for_most_recent; } //reduce according to date order, to give date precedence $diminish_by_date_order = 0.02 * $pcount; $weight = $weight * $diminish_by_date_order; //add weight to the array $weight_array[$post->ID] = $weight; $pcount++; } //if no clicks yet there is no point in randomising if (array_sum($weight_array) > 0 ){ //create a weighted array of values $weight_array2 = array(); foreach($weight_array as $key => $val) { $percent_of_total = round($val / array_sum($weight_array) * 100); for ($x = 0; $x < $percent_of_total; $x++){ $weight_array2[] = $key; } } //select 1000 random items from the weighted array //count each //return an array of IDs and weights $weighted_sort_order_array = array(); for ($x = 0; $x < $post_num; $x++){ $rand_key = rand(0, count($weight_array2)-1); $weighted_sort_order_array[$weight_array2[$rand_key]]++; } $this->weighted_order_array = $weighted_sort_order_array; } else { $this->weighted_order_array = $weight_array; } usort($the_posts,array($this,'sort_posts')); //remove half the posts array_splice($the_posts, $post_num); //add the remainder to the post list foreach($the_posts as $post){ $this->listed_posts[] = $post->ID; } //return what is left return $the_posts; } function sort_posts($a,$b){ $a_val = $this->weighted_order_array[$a->ID]; $b_val = $this->weighted_order_array[$b->ID]; if ($a_vale == $b_val) { return 0; } return ($a_val > $b_val) ? -1 : 1; } } $fun_with_evo_posts = new fun_with_evo_posts(); ?>