Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hey i wanted to display the 5 most popular post by view count in my sidebar, but i don't want to use a plugin, i want the php function

so that i can edit it manually such as adding thumbnails next to the title, any idea where i can find this code

share|improve this question
1  
WordPress does not count views, you will need to code your own view counter and then you will be able to get the list of most viewed. if you want i have a lightweight plugin that does that, gist.github.com/2175805 –  Bainternet Mar 23 '12 at 22:33
    
@Bainternet I was about to post my snippet for that but I think you should instead post your gist as the answer so we can close out this question because that's pretty much spot on what he needs. Not to mention that it'd be useful for others (not an uncommon question). –  userabuser Oct 8 '12 at 7:01
    
added as a new answer –  Bainternet Oct 8 '12 at 15:55
add comment

1 Answer 1

As per @userabuser comment:

<?php
/*
Plugin Name: Simple Post Views
Plugin URI: http://en.bainternet.info
Description: Simple And lightweight plugin to track post views.
Version: 0.1
Author: Bainternet
Author URI: http://en.bainternet.info
*/
if ( !class_exists('PostViews')){
    class PostViews{

        static $meta_key = "_post_views_count"

        //class constractor
        public function __construct(){
            //auto count view on the_content function.
            add_filter('the_content',array($this,'setPostViews'));
            //add shortcodes
            add_shortcode("TOP_VIEWED",array($this,'top_viewed'));
            add_shortcode("GET_VIEWS"array($this,'top_viewed'));
            //add colums to list view
            add_filter('manage_posts_columns', array($this,'posts_columns_id'), 5);
            add_action('manage_posts_custom_column', array($this,'posts_custom_id_columns'), 5, 2);
        }

        //add views column to posts listing
        public function posts_columns_id($defaults){
            $defaults['post_views'] = __('Views');
            return $defaults; 
        }

        //render views column
        public function posts_custom_id_columns($column_name, $id){
            if($column_name === 'post_views'){
                echo $this->getPostViews(get_the_ID());
            }
        }

        //function to get the views
        public function getPostViews($postID = null){
            if (null == $postID){
                global $post;
                $postID = $post->ID;
            }

            $count = get_post_meta($postID, $this->meta_key, true);
            if($count==''){
                delete_post_meta($postID, $this->meta_key);
                add_post_meta($postID, $this->meta_key, '0');
                $re = 0; 
            }else{
                $re = $count;
            }
            return apply_filters("Post_Views_get_views",$re,$postID);
        }

        //function to set the view count
        public function setPostViews($postID = null) {
            if (null == $postID){
                global $post;
                $postID = $post->ID;
            }

            $count = get_post_meta($postID, $this->meta_key, true);
            if($count==''){
                $count = 0;
                delete_post_meta($postID, $this->meta_key);
                add_post_meta($postID, $this->meta_key, '0');
            }else{
                $count++ ;
                update_post_meta($postID, $this->meta_key, $count); 
            }
        }

        //function to get views by shortcode.
        public function top_viewed($atts, $content = null) {
            extract(shortcode_atts(array(
            "id" => null
            ), $atts));
            return $this->getPostViews($id);
        }

        /**
         * shortcode handler for top viewed
         * usage: [TOP_VIEWED count="5"]
         * this will list top five viewed posts
         */
        public function top_viewed($atts, $content = null) {
            extract(shortcode_atts(array(
            "count" => '5'
            ), $atts));

            global $wp_query,$paged,$post;
            $temp = $wp_query;
            $wp_query= null;
            $wp_query = new WP_Query(array(
                'posts_per_page' => $count,
                'order' => 'DESC',
                'orderby' => 'meta_value_num',
                'meta_key' => $this->meta_key
            ));
            ob_start();
            ?>
                <ul class="loop">
                <?php have_posts()) : $wp_query->the_post(); ?>
                <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
                <?php endwhile; ?>
                </ul>
            <?php
            $wp_query = null;
            $wp_query = $temp;
            wp_reset_query();
            $content = ob_get_contents();
            ob_end_clean();

            return $content;
        }

    }//end class
}
$Vcounter = new PostViews();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.