3
votes

I'm working on a wordpress, and using a plugin wordpress popular posts, and facing this issue.

How can I show a count of how many times a page has been viewed by visitors on category listing page or index page of the blog. This information is already there which is being used by plugin to display same in sidebar, not sure how can I use the same plugin data to show page view count on blog page.

I tried to find this, but didn't get exactly what I want.

Pls advise how to do this?

Plugin I use is http://wordpress.org/extend/plugins/wordpress-popular-posts/

3

3 Answers

3
votes

You can simply do it without a plugin.

To count the post views, the first thing you have to do is to add the following code to your WordPress Theme functions.php

<?php
/*
 * Set post views count using post meta//functions.php
 */
function customSetPostViews($postID) {
    $countKey = 'post_views_count';
    $count = get_post_meta($postID, $countKey, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $countKey);
        add_post_meta($postID, $countKey, '1');
    }else{
        $count++;
        update_post_meta($postID, $countKey, $count);
    }
}
?>

And now we will call this function in the single.php to update the count value in the Database.

<?php 
    customSetPostViews(get_the_ID());//single.php
?>

Now in the same single.php file if we want to show the post view count, we can use this code:

<?php
    $post_views_count = get_post_meta( get_the_ID(), 'post_views_count', true );
    // Check if the custom field has a value.
    if ( ! empty( $post_views_count ) ) {
        echo $post_views_count;
    }
?>

Now to show all the popular post in the descending order by post view count. use this code:

<?php//popular post query
    query_posts('meta_key=post_views_count&posts_per_page=5&orderby=meta_value_num&
    order=DESC');
    if (have_posts()) : while (have_posts()) : the_post();
?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
    endwhile; endif;
    wp_reset_query();
?>

Happy Coding

2
votes

There is a function called wpp_get_views provided by the plugin. If you know the ID of the post/page for which you want to show the view count, all you need to do is to call the function with that ID as the parameter. For example:

$count = wpp_get_views($post->ID); 
0
votes
add_action('init','cr_tbl',55);

function cr_tbl(){

        global $wpdb;
    $table_add_two = $wpdb->prefix."post_view";
    $cat_rating_ddl="
        CREATE TABLE IF NOT EXISTS  `".$table_add_two."` (
          `pvid` INT(11) UNSIGNED AUTO_INCREMENT,
          `ip` varchar(255) NOT NULL,
          `device` varchar(255) NOT NULL,         
          `postid` int(11) NOT NULL,
          `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
          PRIMARY KEY(pvid)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    ";  
    $all_tables = array();
    $mytables=$wpdb->get_results("SHOW TABLES");
    foreach ($mytables as $mytable){
        foreach ($mytable as $t){                   
            $all_tables[]=$t;
        }
    }       
    $sql_one='';
    if(!in_array($table_add_two,$all_tables)){
        $sql_one.=$cat_rating_ddl;
        if(!empty($sql_one)){
            if(!function_exists('wp_should_upgrade_global_tables')){
                require(ABSPATH . 'wp-admin/includes/upgrade.php');
            }
            dbDelta($cat_rating_ddl);
        }       
    }

}

function get_the_user_ip() {

    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;}

    function add_view_record($pid){
     global $wpdb;
     $ip = get_the_user_ip();    
     $device = (wp_is_mobile())?'mobile':'desktop';         
     $table_name = $wpdb->prefix.'post_view';
     $list = $wpdb->get_results("SELECT * FROM $table_name WHERE postid = $pid and ip = '".$ip."'");        
     if(empty($list)){      
        $wpdb->insert($table_name, 
            array( 
                'ip' => $ip,
                'device' => $device,
                'postid' =>$pid,                
            )
        );                                   
     }}

    function get_view_count($pid){
    $total=0;
    global $wpdb;       
    $table_name = $wpdb->prefix.'post_view';
    $list = $wpdb->get_results("SELECT count(pvid) as total FROM $table_name WHERE postid = ".$pid);        
    if(!empty($list)){         
           $total=$list[0]->total;  
    }       
    return $total;}