class recentPostViews_Widget extends WP_Widget { function recentPostViews_Widget() { //Constructor $widget_ops = array('classname' => 'recentPostViews_Widget', 'description' => 'Allows you to display recent posts, with the number of views, in the sidebar.'); $this->WP_Widget('recentPostViews_Widget', 'Recent Post Views', $widget_ops); } function form($instance) { //Widget form in backend $instance = wp_parse_args((array) $instance, array( 'title' => '', 'timeframe' => '2', 'numposts' => '7', 'category' => 'ALL', 'hotlevel' => '.75', 'hotcolor' => 'd5000')); $title = esc_attr($instance['title']); $timeframe = intval($instance['timeframe']); $numposts = intval($instance['numposts']); $category = $instance['category']; $hotlevel = $instance['hotlevel']; $hotcolor = $instance['hotcolor']; ?>
How many days old is too old? Expressed as an integer.
'name', 'order' => 'ASC' ); $categories=get_categories($catargs); ?>
Must be expressed as a decimal, e.g., .75. This is the percent threshold, of the most views for a post, to be considered "hot." If 0 is the level, all posts are hot. At 1.0, only the most viewed is hot.
Expressed as a hex color code, no '#', e.g., d5000.
prefix; $homeurl = home_url(); $imageurl = plugins_url('recent-post-views/'); $ndays = $timeframe; $fontcolor = '#'.$hotcolor; $maxViews = $wpdb->get_row("SELECT SUM(".$prefix."postmeta.meta_value) as 'views' FROM ".$prefix."postmeta LEFT JOIN ".$prefix."posts ON ".$prefix."postmeta.post_id = ".$prefix."posts.ID WHERE ".$prefix."postmeta.meta_key = 'views' AND ".$prefix."posts.post_type = 'post' AND ".$prefix."posts.post_date > DATE_SUB(CURDATE(), INTERVAL ".$ndays." DAY) GROUP BY ".$prefix."posts.post_title ORDER BY views DESC LIMIT 1",ARRAY_A); //First check if the user selected a category for the widget or not. Then switch out a query based on that info. if ($category == 'ALL') { $postviews = $wpdb->get_results("SELECT ".$prefix."posts.post_date, ".$prefix."posts.ID, ".$prefix."posts.post_title, ".$prefix."posts.post_name, SUM(".$prefix."postmeta.meta_value) as 'views' FROM ".$prefix."postmeta LEFT JOIN ".$prefix."posts ON ".$prefix."postmeta.post_id = ".$prefix."posts.ID WHERE ".$prefix."postmeta.meta_key = 'views' AND ".$prefix."posts.post_type = 'post' AND ".$prefix."posts.post_date > DATE_SUB(CURDATE(), INTERVAL ".$ndays." DAY) GROUP BY ".$prefix."posts.post_title ORDER BY views DESC LIMIT ".$numposts."",OBJECT); } else { $postviews = $wpdb->get_results("SELECT ".$prefix."posts.post_date, ".$prefix."posts.ID, ".$prefix."posts.post_title, ".$prefix."posts.post_name, SUM(".$prefix."postmeta.meta_value) as 'views', ".$prefix."term_relationships.term_taxonomy_id, ".$prefix."terms.name FROM ".$prefix."postmeta LEFT JOIN ".$prefix."posts ON ".$prefix."postmeta.post_id = ".$prefix."posts.ID LEFT JOIN ".$prefix."term_relationships ON ".$prefix."posts.ID = ".$prefix."term_relationships.Object_ID LEFT JOIN ".$prefix."terms ON ".$prefix."terms.term_id = ".$prefix."term_relationships.term_taxonomy_id WHERE ".$prefix."postmeta.meta_key = 'views' AND ".$prefix."posts.post_type = 'post' AND ".$prefix."posts.post_date > DATE_SUB(CURDATE(), INTERVAL ".$ndays." DAY) AND ".$prefix."terms.term_id = ".$category." GROUP BY ".$prefix."posts.post_title ORDER BY views DESC LIMIT ".$numposts."",OBJECT); } foreach ($postviews as $widgetvalue) { //Open the table and the list. echo'| '; //if the post has the most views, include the "hottest" image. if ($widgetvalue->views == $maxViews['views']) echo ' | '.$widgetvalue->post_title.' - '.$widgetvalue->views.' views | '.$widgetvalue->post_title.' - '.$widgetvalue->views.' views | '; //otherwise, just list the post with a link. else echo ''.$widgetvalue->post_title.' - '.$widgetvalue->views.' views | '; //Close the table and the list. echo '