All pastes #885795 Raw Edit

Display Most Viewed Page/Post By

public php v1 · immutable
#885795 ·published 2008-01-31 03:31 UTC
rendered paste body
### Function: Display Most Viewed Page/Post By Category IDif(!function_exists('get_most_viewed_category')) {	function get_most_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {		global $wpdb, $post;		$where = '';		$temp = '';		if(is_array($category_id)) {			$category_sql = "$wpdb->terms.term_id IN (".join(',', $category_id).')';		} else {			$category_sql = "$wpdb->terms.term_id = $category_id";		}		if(!empty($mode) && $mode != 'both') {			$where = "post_type = '$mode'";		} else {			$where = '1=1';		}		$most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->term_relationships ON $wpdb->term_relationships.object_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND $category_sql AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER  BY views DESC LIMIT $limit");		if($most_viewed) {			if($chars > 0) {				foreach ($most_viewed as $post) {					$post_title = htmlspecialchars(stripslashes($post->post_title));					$post_views = intval($post->views);					$post_views = number_format($post_views);					$temp .= "<li><a href=\"".get_permalink()."\">".snippet_chars($post_title, $chars)."</a> - $post_views ".__('views', 'wp-postviews')."</li>\n";				}			} else {				foreach ($most_viewed as $post) {					$post_title = htmlspecialchars(stripslashes($post->post_title));					$post_views = intval($post->views);					$post_views = number_format($post_views);					$temp .= "<li><a href=\"".get_permalink()."\">$post_title</a> - $post_views ".__('views', 'wp-postviews')."</li>\n";				}			}		} else {			$temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";		}		if($display) {			echo $temp;		} else {			return $temp;		}	}}